Custom Taxonomy Template not respecting ‘include_children’ => ‘false’

There was a lot of hard coding going on in a number of these answers that I felt like may have disadvantages further down the road. Here is an approach that takes the existing tax_query, and swaps out the “include_children” argument.

As an aside, I did not have any luck setting “include_children” to false in this instance. I only found a zero to work.

function wpse239243_exclude_child_taxonomies( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if (is_tax('product-category')) {
            $tax_query = $query->tax_query->queries;
            $tax_query[0]['include_children'] = 0;
            $query->set( 'tax_query', $tax_query );
        }
    }
    return;
}
add_action( 'pre_get_posts', 'wpse239243_exclude_child_taxonomies', 1 );

Leave a Comment