Add Custom Post Type to Tax Query

When register a custom post type with tax “post_tag” or “category”. You don’t see this post type displayed in this tax page. You need filter query (my ex is “product” post type and tax is “post_tag”):

// add post type 'page' to tax query
add_filter( 'pre_get_posts', 'tr_add_pages_to_tax_query' );
function tr_add_pages_to_tax_query( $query )
    if ( ! is_main_query() || ! is_tag()  || !$query->query_vars['tag'])
        return $query;

    $query->set('post_type', array('product','post'));

    return $query;
Comments are closed.