707 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+2 more
ChillThemes says

I noticed that tags and categories weren’t showing up when using 'taxonomies' => array( 'category', 'post_tag' ) on a custom post type. So, I Googled the issue and came across this useful function. The following code is modified to only show the custom post types in the tags and category archive page.

function include_custom_post_types( $query ) { global $wp_query;
    if ( !is_404() && !is_admin() && !is_author() && !is_home() && !is_preview() && !is_singular() ) {
        $post_types = get_post_types();
        $custom_post_type = get_query_var( 'post_type' );
        if ( empty( $custom_post_type ) ) $query->set( 'post_type' , $post_types );
    }
    return $query;
}
add_filter( 'pre_get_posts' , 'include_custom_post_types' );
2445 posts
  • Has referred 100+ members
  • Has sold $100+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+3 more
digitalimpact says

Wouldn’t this be easier (I mean, shorter) ? :)

function include_custom_post_types( $query ) { global $wp_query;
    if ( is_category() || is_tag() ) {
        $post_types = get_post_types();
        $custom_post_type = get_query_var( 'post_type' );
        if ( empty( $custom_post_type ) ) $query->set( 'post_type' , $post_types );
    }
    return $query;
}
add_filter( 'pre_get_posts' , 'include_custom_post_types' );
by
by
by
by
by
by