Modify Taxonomy pages to exclude items in child taxonomies

I know this is an old question, but it is a bit confusing and hopefully will help someone. The reason that `$query->set doesn’t work is because the query has already been parsed and now we need to also update the tax_query object also. Here is how I did it:

function my_tax_query( $query ) {
    $package_id = 12345;
    $tax_query = array(
        'taxonomy' => 'package_id',
        'terms'    => array( $package_id ),
        'field'    => 'slug',
        'operator' => 'IN',
    );
    $query->tax_query->queries[] = $tax_query; 
    $query->query_vars['tax_query'] = $query->tax_query->queries;
}
add_action( 'pre_get_posts', 'my_tax_query' );

Leave a Comment