• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Thomas Griffin

Be a better leader

  • About
  • Newsletter
  • Articles
  • Business
  • Favorites
  • Contact

How to Include Custom Post Types in WordPress Archive Pages

Thomas Griffin Avatar
written byThomas Griffin
posted onJanuary 17, 2021
How to Include Custom Post Types in WordPress Archive Pages

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.

By default, custom post types are not included in archive pages, even if they use the default category and tag taxonomies built into WordPress.

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.

Exclusive WordPress Offer

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!

FacebookTweetLinkedIn
Thomas Griffin Avatar
Thomas Griffin

Hey friend! I'm Thomas, and I've been in WordPress for 10+ years. I've contributed to core and created the TGM Plugin Activation class. My WordPress plugins are active on over 15,000,000 websites and include popular ones like OptinMonster, WPForms and MonsterInsights.

View my WordPress toolbox•More about me →

previous article: How to Remove Empty Paragraph Tags from Shortcodes in WordPress
Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader

Breakthrough Leadership

Great leaders share common traits that separate them from the pack. Download your copy of my signature ebook, Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader, absolutely FREE when you enter your name and email address below.

Action leads to insight more often than insight leads to action.

Thomas Griffin

Footer

ThomasGriffin.com Logo

Copyright © 2010-2021 Griffin Media LLC.
All rights reserved.

Connect

Sites I Like
OptinMonsterTrustPulseWPFormsMonsterInsightsWPBeginnerEclectic TigerSyed BalkhiAwesome Motive

Misc
WordPressPrivacyDisclosures Sitemap

Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader

Breakthrough Leadership

Great leaders share common traits that separate them from the pack. Download your copy of my signature ebook, Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader, absolutely FREE when you enter your name and email address below.