Code Snippet

Home » Code Snippets » WordPress » Make Archives.php Include Custom Post Types

Make Archives.php Include Custom Post Types

Archives.php only shows content of type 'post', but you can alter it to include custom post types. Add this filter to your functions.php file:

function namespace_add_custom_types( $query ) {
  if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'your-custom-post-type-here'
		));
	  return $query;
	}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

Subscribe to The Thread

  1. Karim

    Can I make the search to include custom posts too in the results?

    • Yes.

      Put this in your functions.php

      // Define what post types to search
      function searchAll( $query ) {
      	if ( $query->is_search ) {
      		$query->set( 'post_type', array( 'post', 'page', 'feed', 'custom_post_type1', 'custom_post_type2'));
      	}
      	return $query;
      }
      
      // The hook needed to search ALL content
      add_filter( 'the_search_query', 'searchAll' );

      I also assume you already added ‘publicly_queryable’ => true, to the $args array of your CPT.

      hope it helps

  2. I am having issues in using this snippet. It gives me white screen of depth. I tried on wordpress forums too. But cant get to display custom post types in archives

  3. Thanks for posting this! It worked a treat.

  4. alamedagraphik

    thanks for this code snippets to archived the custom post type.
    In the past i used a pluggin (“Simple custom post type archives”) to archive the custom post type and in my theme i had to use a file to display all custom posts type archive.
    but now it’s more simple!
    thank you again

  5. Can you give a slight bit more detail like…

    How does it know to only do this on the archives page? etc….

    Its a short snippet so I don’t think it would be much trouble. Thanks in advance.

  6. David

    Solved that problem – getting custom post types to display in an archive page – however mega menu goes blank, some images not displaying on welcome page, welcome page slider menu/nav is not working…

    WP3.2.1, Magazinum Theme, Design Chemical Mega Menu, Custom Post Types UI

  7. Thanks but I don’t think this addition to functions.php file will really altar to include custom post types. I agree with Cav. There must be a detailed tutorial.

  8. thanks..

    i did it :)

    if( is_home() || is_tag() && empty( $query->query_vars['suppress_filters'] ) )

  9. Also include ‘nav_menu_item’ if you don’t want your menus to disappear!

    function namespace_add_custom_types( $query ) {
      if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
         'post', 'nav_menu_item', 'your-custom-post-type-here'
    		));
    	  return $query;
    	}
    }
    add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
  10. Thanks for the menu tip, Jon B – just what I was looking for.

  11. I have 1 CPT called as “product”. This CPT has unique format/design ( not like regular wordpress posts ) but has same capability ( tags and categories). How can I separate this custom post type in homepage, archive, etc. ???

    Thanks, :)

  12. Love it! Worked like a charm.
    Thanks guys!

  13. with suppress filters you can affect also the attachment pages

    A possible solution
    function my_get_posts( $query ) {
    if(!is_admin() || is_archive()){
    if (!is_post_type_archive() && $query->is_main_query() )
    $query->set( ‘post_type’, array( ‘post’, ‘page’, ‘post-format’, ‘video’, ‘slideshow’, ‘timeline’ ) );

    return $query;
    }
    }
    !is_admin – without this are affected also the searches in admin pages.
    A demo here:
    http://www.zurita-bach.com/
    At this moment I have only timeline as custom post

  14. Replace $query->$query->is_main_query()
    with $query->is_archive()

  15. Riss

    Thanks Jon B you are the boss!

  16. alvar

    I’ve found that this creates a problem querying a post_type and a cat.

    You can see this in the backend by visiting a category list for a custom post type:
    Click the Category menu item in a custom type, then click the number to see the posts in that cat.
    wp-admin/edit.php?category_name=some_cat&post_type=some_type

    You’ll see the posts displayed seem to be every type BUT the queried one.
    Even with Ana’s solution that is supposedly excluding admin.

  17. Hi alvar my final solution
    add_filter( ‘pre_get_posts’, ‘my_get_posts’ );

    function my_get_posts( $query ) {
    if(!is_admin()){
    if (!is_post_type_archive() && $query->is_archive())
    $query->set( ‘post_type’, array( ‘post’, ‘page’, ‘post-format’, ‘video’, ‘slideshow’, ‘timeline’ ) );

    return $query;
    }
    }

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~