Code Snippet
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
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!
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;
}
}
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.