This tutorial on how to include custom post types in WordPress archive pages is related to my previous article on how to include custom post types in WordPress search results.
We’ll learn how to get custom post types to appear in your WordPress archives by modifying the main query via the pre_get_posts
hook.
Key Takeaways
- By default, custom post types are not included in archive pages, even if they use the default category and tag taxonomies built into WordPress.
- In this step-by-step tutorial, you are going to create a function that makes custom post types display in your WordPress archive pages.
Building the Function to Include Custom Post Types in WordPress Archive Pages
Open up your functions.php
file and paste in the following snippet of code:
/**
* This function modifies the main WordPress archive query for categories
* and tags to include an array of post types instead of the default 'post' post type.
*
* @param object $query The main WordPress query.
*/
function tg_include_custom_post_types_in_archive_pages( $query ) {
if ( $query->is_main_query() && ! is_admin() && ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) ) {
$query->set( 'post_type', array( 'post', 'movies', 'products', 'portfolio' ) );
}
}
add_action( 'pre_get_posts', 'tg_include_custom_post_types_in_archive_pages' );
What does this function do? It modifies the primary frontend WordPress query when you are in a category or tag context. Then, it injects the custom post types you define into the query itself, allowing them to be shown on category and tag archive pages. The above function will return content from each of these custom post types: post
, movies
, products
and portfolio
.
Change the names of the custom post types in the array to match your custom post types that you want included in your WordPress archive pages.
Want a WordPress website that’s secure AND fast? My friends at WP Engine are offering 3 months free on all annual plans. Click here to claim your special WP Engine offer!
And that’s it! You have successfully included custom post types in your WordPress archive pages!
I truly hope this tutorial was helpful, and don’t forget to subscribe to get access to more great WordPress content!
Hervé
Thanks for the code!
And don’t forget to leave
post
when adding your CPT. 😉Thomas Griffin
You’re welcome, Hervé — and indeed, you do need to leave
post
so that your blog posts continue to show up in those archive pages!Michelle
So helpful! I was wondering how, if possible, you could get custom post types to show up in your posts feed along with posts.
ie. I have a blog with blog posts, and I have a custom post type for quizzes. I want the quizzes and posts to both show up in my latest posts feed.
Thomas Griffin
Hey Michelle,
Glad you found it helpful! If you’d like to include custom post types in your main blog feed, you can use the following function:
Hope that helps!
Michael
Thank you Thomas! Very much.
Steven Wilson
Thank you! I was actually looking for something like this. Quick question….how do you get these custom post types to use a custom archive template? Seems like it uses the default archive.php template but not the one i created…. resources-archive.php.
Thomas Griffin
Thanks Steven!
In order to get custom post types to use a custom archive template, you need to make sure the name of the template matches the exact slug of the custom post type. And, the way it is named is like this:
archive-{custom_post_type}.php
. It seems you’ve got it backwards, so if you fix that, it should work.Jubee
You saved my day. Thanks a lot!
Thomas Griffin
You’re welcome, Jubee!
cc
You solved a big problem for me, thank you, Thomas!