Show Custom Post Types on the Homepage

Custom post types are commonly used in WordPress sites, but they don’t show on the homepage or in the archive pages unless you write code to do that. I’ve put together some code that is smart about detecting your public custom post types and automatically adding them to your homepage and archive pages:

 * Show WordPress custom post types on the main blog and archive pages
function show_my_post_types( $query ) {

  // Show all custom post types on main blog and archive pages
  if ( $query->is_main_query() && ( is_home() || ( is_archive() && !is_post_type_archive() ) ) ) {

    $custom_post_types = get_post_types( array(
       'public' => true,
       '_builtin' => false,
    ) );

    $post_types = array_merge( array('post'), $custom_post_types );
    $query->set( 'post_type', $post_types );

   return $query;

add_filter( 'pre_get_posts', 'show_me_post_types' );

Create WordPress Custom Post Type Archive PageTemplates

Post Type Templates

Custom Post Type Templates

  • archive-{post_type}.php
  • single-{post_type}.php
If the custom post type were ‘product’, WordPress would look for archive-product.php to display the archive of posts.
If the custom post type were ‘product’, WordPress would look for single-product.php to display the single or permalink of the post.

If these files are not available in your theme WordPress will look for archive.php and or single.php respectfully. If they are not present it will default to index.php.

How to Create a Custom Post Types Archive Page in WordPress

If you want to show your custom post types on your category archive pages?

Add this to your functions.php file:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if(is_category() || is_tag()) {
    $post_type = get_query_var('post_type');
	    $post_type = $post_type;
	    $post_type = array('post','myCustomPostTymeName'); // replace myCustomPostTymeName to your custom post type
	return $query;

Now change the variable ‘myCustomPostTymeName’ to the registered name of your custom post type, and Enjoy!