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' );
Can I make the search to include custom posts too in the results?
Yes.
Put this in your functions.php
I also assume you already added ‘publicly_queryable’ => true, to the $args array of your CPT.
hope it helps
Oh man, I’ve been hacking at this for two days straight! THANK YOU for this simple to implement solution!
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
Thanks for posting this! It worked a treat.
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
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.
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
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.
thanks..
i did it :)
if( is_home() || is_tag() && empty( $query->query_vars['suppress_filters'] ) )
Also include ‘nav_menu_item’ if you don’t want your menus to disappear!
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
Yes, including “nav_menu_item” was the key for me. Thanks so much, John B.
Thanks for the menu tip, Jon B – just what I was looking for.
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, :)
Love it! Worked like a charm.
Thanks guys!
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
Replace $query->$query->is_main_query()
with $query->is_archive()
Thanks Jon B you are the boss!
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.
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;
}
}
Saved my day. Thanks!
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?
Thanks for the code…It worked as treat for me…And it is the same i was looking for:)
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.
This code snippet saved my bacon… one question… how would you include additional arguments like &orderby=title and &order=asc? Thanks!
This worked best for me:
It kept the navigation, my widgets and custom query on the home page intact.
Daan’s Solution works perfect.
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
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.
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:
And that should fix the issue! :) It did on my end anyways, so I hope it helps others!
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’)
);
}
add_action(‘pre_get_posts’, ‘view_AllCustomPost’);
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.
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
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:
Hope this helps someone, because it was a bear to figure out ; )