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', 'nav_menu_item', 'your-custom-post-type-here'
		));
	  return $query;
	}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

Comments

  1. User Avatar
    Karim
    Permalink to comment#

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

    • User Avatar
      Rui
      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

    • User Avatar
      Paul
      Permalink to comment#

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

    • User Avatar
      Christopher Curtin
      Permalink to comment#

      THANK YOU! Couldn’t figure this one out!

  2. User Avatar
    Mohit Kumar
    Permalink to comment#

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

    Thanks for posting this! It worked a treat.

  4. User Avatar
    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. User Avatar
    Cav
    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. User Avatar
    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. User Avatar
    Imran Hunzai
    Permalink to comment#

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

    thanks..

    i did it :)

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

  9. User Avatar
    Jon B
    Permalink to comment#

    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' );
    • User Avatar
      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

    • User Avatar
      SM
      Permalink to comment#

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

    • User Avatar
      Adam
      Permalink to comment#

      Thanks Jon. Lifesaver.

    • User Avatar
      Michael Schofield
      Permalink to comment#

      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.

    • User Avatar
      Jeremy
      Permalink to comment#

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

    • User Avatar
      Prasanth
      Permalink to comment#

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

    • User Avatar
      Grant
      Permalink to comment#

      you save me me Jon, thanks a lot.

    • User Avatar
      Josh Larson
      Permalink to comment#

      Thanks, Jon!

    • User Avatar
      andrew
      Permalink to comment#

      Thanks for pointing that out Jon B! Chris you should update your snippet to include Jon’s suggestion.

    • User Avatar
      Chris Coyier
      Permalink to comment#

      I’ll update the post, since this is so obviously better. =)

    • User Avatar
      Tobias
      Permalink to comment#

      I think this should be resolved by adding

      && $query->is_main_query()
      

      to your conditions

  10. User Avatar
    Marcus J Wilson
    Permalink to comment#

    Thanks for the menu tip, Jon B – just what I was looking for.

  11. User Avatar
    Sangsaka
    Permalink to comment#

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

    Love it! Worked like a charm.
    Thanks guys!

  13. User Avatar
    Ana

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

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

  15. User Avatar
    Riss

    Thanks Jon B you are the boss!

  16. User Avatar
    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. User Avatar
    Ana

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

    • User Avatar
      Daline27
      Permalink to comment#

      @Ana : Thanks very much, this (&& $query->is_) solved my problem with recent posts widget (before all type post are shown).

    • User Avatar
      Yiorgos
      Permalink to comment#

      I tested every piece of code here and this was the only one that worked perfectly. Thanks Ana!!!
      I’ll just put it in a code block for easy copy/paste:

      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', 'your_custom_post_type' ) );
              }
              return $query;
          }
      }
      
    • User Avatar
      Michael
      Permalink to comment#

      Wow. ANA this just saved my butt. This worked for me.

    • User Avatar
      Elle
      Permalink to comment#

      Thanks Ana!!

  18. User Avatar
    Iris
    Permalink to comment#

    Saved my day. Thanks!

  19. User Avatar
    Gus
    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. User Avatar
    deepak
    Permalink to comment#

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

  21. User Avatar
    Gus
    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. User Avatar
    leandra
    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. User Avatar
    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.

    • User Avatar
      manmar
      Permalink to comment#

      Thanks Daan!

    • User Avatar
      Iris
      Permalink to comment#

      You’re a genious! Thank you so much!

    • User Avatar
      Michelle

      Thanks Daan! This was the fix for me too.

    • User Avatar
      Michelle

      Should have mentioned, I added
      !is_admin()

      // Add custom post types to archives
      function custom_post_archive($query) {
          if ( !is_admin() && $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');
      

      to the conditional so that it wouldn’t pull all my custom post types in the control panel as well as on the archive pages.

  24. User Avatar
    Elshereef
    Permalink to comment#

    Daan’s Solution works perfect.

  25. User Avatar
    Nicola Elvin
    Permalink to comment#

    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

    • User Avatar
      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.

    • User Avatar
      Lindsey
      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. User Avatar
    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. User Avatar
    David
    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. User Avatar
    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. User Avatar
    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. User Avatar
    Tom
    Permalink to comment#

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

    Thanks!

    • User Avatar
      Tom
      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. User Avatar
    JP Lew
    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. User Avatar
    Alec Rippberger
    Permalink to comment#

    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. User Avatar
    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. User Avatar
    Alec Rippberger
    Permalink to comment#

    Glad you found it helpful Ben!

  35. User Avatar
    Jeremy
    Permalink to comment#

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

  36. User Avatar
    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. User Avatar
    Adam
    Permalink to comment#

    Awesome! Thanks!

  38. User Avatar
    William Sousa

    Hey guys.

    Good example!

    It’s possible make category.php includes custom post type?

    Hugs!

  39. User Avatar
    BigLeslieP
    Permalink to comment#

    Once again, dude. You totally rock.

  40. User Avatar
    Axes
    Permalink to comment#

    Thank you.
    Nice tip.

  41. User Avatar
    Rohit Tripathi
    Permalink to comment#

    Pretty Neat Trick. Thanks :)

  42. User Avatar
    Ray1618
    Permalink to comment#

    Jon B’s & Daans solutions worked both for me, great! Thx!
    Why doesn’t this work out of the box?!? Kept me searching for hours :S

  43. User Avatar
    Bart De Vuyst
    Permalink to comment#

    This snippet doesn’t work with WPML. Does anyone have an idea why it doesn’t work on multi-language websites?

    thanks,

    Bart

    • User Avatar
      Emilien
      Permalink to comment#

      I have the same problem… did you come up to a solution? Have you contacted the WPML support team?
      Is anyone else facing the same issue ? Does anyone have an idea or a solution ?

      Thank you so much for your help, snippet works great without WPML ;)

      Emilien

    • User Avatar
      Bart De Vuyst
      Permalink to comment#

      Hi Emilien,

      I got this fixed. I also reported this to WPML but they kinda ignored the issue… here’s the thread and how I fixed it: https://wpml.org/forums/topic/wpml-breaks-category-archives-for-custom-post-types/

    • User Avatar
      Emilien
      Permalink to comment#

      Hi Bart and thank you for your message,

      I finally solved this but in a different way than you did. Here is the snippet I came up to.

      query_vars['suppress_filters'] ) ) {
          $query->set( 'post_type', array(
           'post', 'nav_menu_item', 'my_custom_post_type', 'my_other_custom_post_type'
              ));
          $query->set('suppress_filters', true); //wpml filter
            return $query;
          }
      }
      add_filter( 'pre_get_posts', 'custom_function_add_custom_post_types' );
      ?>
      

      Thank you again for your help, have a great day!

      Emilien

  44. User Avatar
    Mukama Ivan
    Permalink to comment#

    Thanks fro the post. Is it possible to add items/categories from the custom post types to the default WordPress Archive widget? It only shows posts.

  45. User Avatar
    Vlada
    Permalink to comment#

    Thanksfor this post, and see menu ppl solve the problem, but i don’t :(
    I making my own theme and i successfully make 2 custom post type and i displaying in my blog. Only problem is my arhive.php shows only regular posts, not showing my custom post typs (same after using this code)
    can someone tell me where what file i must also check to solve this problem?
    Thanks..

  46. User Avatar
    Develocom
    Permalink to comment#

    I agree with Vlada. Does anyone have a solution that works on the Main Blog, Category pages, and Archive pages? If that was posted, my apologies but I can’t seem to find the solution anywhere.

  47. User Avatar
    hj
    Permalink to comment#

    how to link a custom post page to archive page

  48. User Avatar
    Develocom
    Permalink to comment#

    The problem with any of the solutions above that I didn’t see mentioned by anyone is the fact that everything works until you get to a custom post type archive. It shows all the posts like a main feed. Here is the final working solution on all WordPress pages. If you see any errors or find something better, let me know :)

    function some_function( $query ) {
      if(!is_admin()) {
    
      // For categories, lets add 'nav_menu_item' to the array so we don't break our navigation.//
      if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
         'post', 'nav_menu_item', 'custom_post_type', 'another_custom_post_type'
        ));
        return $query;
      }
      if( is_archive() && !is_post_type_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
         'post', 'custom_post_type', 'another_custom_post_type'
        ));
        return $query;
      }
      if( is_home() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
         'post', 'custom_post_type', 'another_custom_post_type'
        ));
        return $query;
      }
     }
    }
    add_filter( 'pre_get_posts', 'some_function' );
    
    • User Avatar
      Mark
      Permalink to comment#

      I was having problems with the original code causing a problem with the filters on regular posts, “invalid post type”. However, Develocom’s updated snippet here works.

    • User Avatar
      Dale Lature
      Permalink to comment#

      Yes!!!!!!!! After a day and a half, FINALLY something that works for me!

  49. User Avatar
    adjmedia
    Permalink to comment#

    Hello, I’m having the same trouble as everyone else here where my nav menus have disappeared. What’s weird is that everything was fine on my local installation as I was building this theme, but when I installed the theme on a server, that’s when my menus disappeared. If anyone has any insight, I’d love it.

    The site is here.

    Here is the code I’m using, adapted from this thread:

    <?php 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', 'section-1', 'section-2', 'section-3', 'section-4', 'section-5', 'section-a', 'section-b', 'section-c', 'section-d', 'nav_menu_item' ) );
            }
            return $query;
    
        }
    }
    
    echo '<h2>';
    echo the_category();
    echo '</h2>';
    ?>
    
  50. User Avatar
    toiber
    Permalink to comment#

    Hi,

    is possible to define or limit a number of posts per page within the same piece of code?

    Thanks,

  51. User Avatar
    Mark
    Permalink to comment#

    This was very helpful, thanks! It worked for me.

  52. User Avatar
    amy
    Permalink to comment#

    I was trying the different solutions provided and was coming up with some issues, depending on version. The biggest problem was with my masonry layout – anyway I combined ideas and all my problems were solved – no nav menu issue, no masonry layout issue weirdness and all appearing where it needed to be sorry this is long I am just so HAPPY. In case it can help someone else – here it is…

    //add custom post types to archives
    
    function add_cpt_to_archives( $wp_query ) {
        $types_array = array( 'post', 'articles','factsheets','questions', 'events' );
        if( is_category() || is_tag() ) {
            set_query_var( 'post_type', $types_array );
        }
    }
    add_action('pre_get_posts', 'add_cpt_to_archives');
    

    still styling the site a bit but you can see it here

    • User Avatar
      Julie
      Permalink to comment#

      THANK YOU AMY!! I’ve been working on this for way too long now, and your solution was perfect!! I had trouble in various ways with all the rest of the code snippets above. Thank you again for sharing! You’re awesome!! :D

  53. User Avatar
    Betty

    Can somebody help me? I am customizing the search – but my navigation disappears when I do the customization..

    // Gets all child pages of parent and search in these post parents.
    // Only when 'parent' URL param is available.
    function parent_search( $query ){
      if(get_query_var('parent')) {
        $pageids = array();
        $childpages = get_pages('child_of='.get_query_var('parent'));
        foreach($childpages as $page) {
          $pageids[] = $page->ID;
        }
        $query->set('post_parent__in', $pageids);
        $query->set('post_type', array('post', 'page', 'nav_menu_item')); // even if I do this, it disappears
      }
      return $query;
    }
    add_filter( 'pre_get_posts', 'parent_search' );
    
  54. User Avatar
    Ukubi

    Hello,

    Widget-archive and widget-calendar are empty if there are only custom posts type (no blog/default posts). I can’t see the month in the archives or click on a day in the calendar if there is not at least one blog post for the period. The page archive is ok (show all posts with the same tag or date)

    Is there a way to use the widgets when there are only custom posts?

    I try many things.. I use this code :

    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’,’portfolio’ ) );

    return $query;
    }
    }

  55. User Avatar
    Ken
    Permalink to comment#

    Thank you for this.
    To save myself having to manually enter each custom post type, I did the following to automatically add any public post types and it seems to work.

    [functions.php]
    
    function hmwd_add_custom_post_types_to_query($post_types){
      $added = get_post_types(array( 'public'=>true,'_builtin'=>false));
      foreach($added as $key=>$value){
        $post_types[]=$key;
      }
      return $post_types;
    }
    function hmwd_modify_archive_query( $query ) {
      if( is_category() || is_tag() || is_date() && empty( $query->query_vars['suppress_filters'] ) ) { 
        $post_types = hmwd_add_custom_post_types_to_query(array( 'post', 'nav_menu_item'));
        $query->set( 'post_type', $post_types);
        return $query;
      }
    }
    add_filter( 'pre_get_posts', 'hmwd_modify_archive_query' );
    
    

    Should make themes easier to maintain as future post types are added.

  56. User Avatar
    Alshe Dupur
    Permalink to comment#

    I tried with all of code. After tried several times… this code is work for me. Here is my code:

    function namespace_add_custom_types( $query ) {
        if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
           $query->set( 'post_type', array('nav_menu_item', 'post', 'post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5', 'post_type_6', 'post_type_7') );
           return $query;
        }
    }
    add_filter( 'pre_get_posts', 'namespace_add_custom_types');
    
  57. User Avatar
    Paul
    Permalink to comment#

    Hello Chris,

    Here is a plugin I made that can help to create a custom archive of any post_type / taxonomy. You can choose from many base elements like cards, badges, pin-card (Pinterest like layout). Then, you can filter any custom post based on post type, taxonomy, etc. Finally you can make any css changes to your “elements”. All changes are shown in a live preview.

    Here is it: http://www.pauple.com/elementary/

  58. User Avatar
    Itay Banner
    Permalink to comment#

    OMG. If only I Googled it 2 hours ago…. Thanks!

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag