Add Custom Post Type To Tag And Category Archive Pages

Here is how to modify the query for default taxonomy archive pages (tag archive page or category archive page) to display custom post types.

/**
 * Add custom post types to the default tag query
 * 
 * This will make sure that the custom post types shows up in the tag archive pages.
 * The idea here is to list both normal posts and cpt named doc under the same tag name.
 *
 * @refer https://wordpress.stackexchange.com/a/285162/90061
 */
function add_cpt_to_defalt_tax_archive( $query ) {
 	
  if ( is_tag() && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
 		
  $query->set( 'post_type', array(
       'post', 'doc'
    ));
  }
  return $query;
}
add_filter( 'pre_get_posts', 'add_cpt_to_defalt_tax_archive' );

Replace doc with your custom post type.

Read more on how you can use default taxonomies with custom post types here: https://wordpress.stackexchange.com/a/92436/90061

Hello, I am Arun Basil Lal. Thank you for reading!

I am a WordPress product developer and creator of Image Attributes Pro. I am passionate about solving problems and travelling the world.

Divi WordPress Theme - My Review

Divi WordPress Theme
Divi is a WordPress theme that web designers do not want you to know. It comes with a drag-and-drop theme builder. You can build beautiful looking unique websites without touching a line of code. Just choose from one of the many pre-made layouts, or pick elements and arrange them any way you like.

Divi is every WordPress developer's wet dream. Surprise your clients with neat responsive websites and have fun building them.

Divi comes from Elegant Themes. If you enjoy building websites, you *need* an Elegant Themes membership. 87 beautiful themes and 5 plugins for the cost of less than a candy-bar each!


Note: I am an avid user of Divi myself and this is a honest review. I wouldn't recommend something that I do not personally find amazing.

3 Comments.

  1. Brian Hughes says:

    Thanks for this! It sure helped simplify things for me 🙂

  2. Jorix says:

    2020 and this code snippet is still very useful. Many thanks, Arun.

Leave a Reply to Jorix Cancel reply

Your email address will not be published. Required fields are marked *

*