707 posts
  • Sold between 10 000 and 50 000 dollars
  • Referred between 10 and 49 users
  • Bought between 10 and 49 items
  • Has been a member for 5-6 years
  • Exclusive Author
  • Microlancer Beta Tester
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' );
2429 posts
  • Has been a member for 4-5 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Microlancer Beta Tester
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