Add Custom Post Type To Tag And Category Archive Pages

[ad_1]

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

[ad_2]

Leave a Reply

Random Post Selection ::

Recent Posts

Unlock Unlimited Downloads