709 posts
  • Affiliate Level 2
  • Author Level 5
  • Beta Tester
  • Collector Level 2
+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' );
2468 posts
  • 6 Years of Membership
  • Author Level 2
  • Affiliate Level 4
  • Beta Tester
+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