Grow your CSS skills. Land your dream job.

Last updated on:

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' );

Comments

  1. Karim
    Permalink to comment#

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

    • Permalink to comment#

      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

    • Paul
      Permalink to comment#

      Oh man, I’ve been hacking at this for two days straight! THANK YOU for this simple to implement solution!

  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. Permalink to comment#

    Thanks for posting this! It worked a treat.

  4. alamedagraphik
    Permalink to comment#

    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. Permalink to comment#

    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
    Permalink to comment#

    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. Permalink to comment#

    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' );
    • Thomas
      Permalink to comment#

      Hi and thanks for the code snippet!

      I have a theme which displays recent posts on the author.php template and unfortunately nav_menu_items are displayed as posts (with empty content). Without nav_menu_item the navigation menu is gone so I’m looking for something that adds custom post types to my archives without integrating the nav_menu_items anywhere else…

      I couldn’t find any solution so far so any help would be highly appreciated!

      Cheers,
      Thomas

    • SM
      Permalink to comment#

      Yes, including “nav_menu_item” was the key for me. Thanks so much, John B.

    • Permalink to comment#

      Thanks Jon. Lifesaver.

    • Jon, thank you! I used the original snippet, realized my menu had blasted, and couldn’t understand why. Incredibly little information about this–although people are having the same problem–with a cursory Google. I had forgotten where I got the snippet from, happened back on Chris’s page, was like, “Wow. It’s the exact same code. Must be where I ganked it,” then found your comment.

      That did the trick. Holy exhausted-but-grateful batman.

    • Permalink to comment#

      Saving grace. Couldn’t figure out why my nav menu’s were disappearing. Thanks.

    • Permalink to comment#

      Thanks a ton, Jon. Saved me a lot of time!

    • Grant
      Permalink to comment#

      you save me me Jon, thanks a lot.

  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. Permalink to comment#

    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. Ana

    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;
    }
    }

  18. Iris
    Permalink to comment#

    Saved my day. Thanks!

  19. Permalink to comment#

    I think Ana’s code is getting close to a solution.

    The problem on my end is that I have widgets that display custom post types. E.g. “Random Testimonial”, “Latest Tip” etc.

    On the pages where this function is being used (the category archive for my custom post type), these widgets are getting confused, and show the content of a different post type – so the random testimonial widget might instead show a random tip.

    Any idea what I can do, either to this function or to my widgets, to get them to play nice together?

  20. deepak
    Permalink to comment#

    Thanks for the code…It worked as treat for me…And it is the same i was looking for:)

  21. Permalink to comment#

    Ok, my problem was that this was making all my widgets (E.g. latest testimonial) pull the wrong post type.

    Here’s the code I solved it with.

    
    /* Allow ht_news post type to use category template without affecting widgets
    ======================================================= */
    function my_get_posts( $query ) {
    	if(!is_admin()) {
    		/*
    		if ht_news is already in the $post_types array then this function has probably already been executed
    		and we don't want to run it again, because that makes all our widgets display ht_news instead 
    		of their own post type
    		*/
    		$post_types = get_query_var('post_type');
    		if(in_array('ht_news',$post_types)) {
    	    	$post_type = 'ht_news';
    	    }
    		if (!is_post_type_archive() && $query->is_archive() && $post_type != 'ht_news') {
    			$query->set('post_type',array('ht_news','nav_menu_item') );
    			return $query;
    		}
    	}
    }
    add_filter( 'pre_get_posts', 'my_get_posts' );
    
  22. Permalink to comment#

    This code snippet saved my bacon… one question… how would you include additional arguments like &orderby=title and &order=asc? Thanks!

  23. Daan
    Permalink to comment#

    This worked best for me:

    // Add custom post types to archives
    function custom_post_archive($query) {
        if ($query->is_archive)
            $query->set( 'post_type', array('your_custom_type_here', 'nav_menu_item', 'post') );
        remove_action( 'pre_get_posts', 'custom_post_archive' );
    }
    add_action('pre_get_posts', 'custom_post_archive');
    

    It kept the navigation, my widgets and custom query on the home page intact.

  24. Permalink to comment#

    Daan’s Solution works perfect.

  25. Works a treat that last post, thanks! However…it now means in the wp-admin, in each custom post list ui, all the different custom post types show up apart from just that one we have clicked on

    • Andrew
      Permalink to comment#

      This same thing happens for me, as well. I’m using Daan’s code and if I can fix this small issue, it’ll be exactly what I need.

    • Permalink to comment#

      Granted I know this comment is about 4 months old, but I did figure out how to fix this issue.

      In your functions.php file where you have created your custom post type, change this line:

      FROM:
      'has_archive' => true,
      
      TO:
      'has_archive' => false,
      

      And that should fix the issue! :) It did on my end anyways, so I hope it helps others!

  26. Xavier
    Permalink to comment#

    The code Dann is perfect for troubleshooting with Widgets, but give me a problem when displaying Custom Post Type on the menu if besides “is_category” added “is_tag” solve it this way:

    function view_AllCustomPost($query) {
    if ($query->is_category) $query->set( ‘post_type’, array(
    ‘post’, ‘nav_menu_item’, ‘custom_post_type’)
    );

    elseif ($query->is_tag) $query->set( 'post_type', array(
            'post', 'nav_menu_item', 'custom_post_type')
    );
    remove_action( 'pre_get_posts', 'view_AllCustomPost' );
    

    }
    add_action(‘pre_get_posts’, ‘view_AllCustomPost’);

  27. Permalink to comment#

    We were recently tasked with updating a legacy WP site and this thread helped a treat, our navigation items disappeared though so make sure to add “nav_menu_item” back in.

  28. Eric Curtis
    Permalink to comment#

    I had better luck modifying the main query.

    http://wordpress.stackexchange.com/questions/27104/how-to-display-regular-posts-custom-post-types-that-fall-under-a-category-usin

    add_action('pre_get_posts', 'add_my_custom_post_type');
    
    /**
     * @param WP_Query $query
     * @return WP_Query
     */
    function add_my_custom_post_type($query) {
    if(
        empty($query->query['post_type'])
        or $query->query['post_type'] === 'post'
    ){
        $query->set('post_type', array('post', 'my_custom_type'));
    }
    }
    
  29. Eric Curtis
    Permalink to comment#

    Ok my above comment did not work after all. Where I have been running into problems is on an archive page for a custom post type. I was able to get everything working with this:

    function custom_post_archive($query) {
    if(!is_admin()) {
    if (!is_post_type_archive() && $query->is_archive())
      $query->set( 'post_type', array('projects', 'nav_menu_item', 'post','team') );
        remove_action( 'pre_get_posts', 'custom_post_archive' );
        }
    }
    add_action('pre_get_posts', 'custom_post_archive');
    

    Hope this helps someone, because it was a bear to figure out ; )

  30. Permalink to comment#

    Daan’s solution worked for me with WooThemes. I just added the function to my custom plugin file.

    Thanks!

    • Permalink to comment#

      Actually, Daan’s code didn’t work properly with my author archive pages so I had to remove ‘nav menu item’ and ‘post’ from the array. My final working code is:

      if ( ! function_exists( 'custom_post_archive' ) ) {
          function custom_post_archive( $query ) {
              if ( $query->is_archive )
                  $query->set( 'post_type', array( 'villa_listings' ) );
              remove_action( 'pre_get_posts', 'custom_post_archive' );
          }
      }
      add_action( 'pre_get_posts', 'custom_post_archive' );
      
  31. Permalink to comment#

    Here’s an example of some alternate syntax—more elegant imo—for adding a custom post type called “lecture” to your taxonomy and date archives:

    function add_lecture_to_archives( $wp_query ) {
    
        $types_array = array( 'post', 'lecture' );
    
        if( is_archive() && is_date() ) {
            set_query_var( 'post_type', $types_array );
        }
    }
    add_action('pre_get_posts', 'add_lecture_to_archives');
    
  32. If you want to include all post types you could use get_post_types() to pull all custom post types dynamically:

    function namespace_add_custom_types( $query ) {
        if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
            $post_types = get_post_types( '', 'names' ); 
            $query->set( 'post_type', $post_types);
            return $query;
        }
    }
    add_filter( 'pre_get_posts', 'namespace_add_custom_types');
    
  33. selno
    Permalink to comment#

    this one helped me

    `$args = array( ‘posts_per_page’ => 10,’post_type’=>’videos_’, ‘category’ => 3 );

    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>

    <a href="”>

    <?php endforeach; `

  34. Glad you found it helpful Ben!

  35. Permalink to comment#

    This bit of code saved my life. Thanks a million.

  36. Lucas
    Permalink to comment#

    Working on a custom site for the company I work with, using wordpress, but my director wants to have the posts behave like pages, and the pages behave like posts. I have this done so far, but I am [well, if I had hair, I would be... but I digress] pulling my hair out trying to figure out how to make it so that when you click on the menu item that would normally feed off of the archive.php file, and grab the posts, our content is being hosted in pages, so this is showing nothing matches the search,… returning no values.

    I have tried all the snippets listed here, to no avail, and am a beginner at best when it comes to php. All I want, is for pages to present within the archive feature, like posts would!

    Help!?

  37. Permalink to comment#

    Awesome! Thanks!

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".