Custom Taxonomy Template not respecting 'include_children' => 'false'

I’ve created a custom taxonomy, for a custom post type, and created a custom page for it, the usual, etc.

Problem is I want to only show the custom posts that are part of the category, and not show the posts from the sub categories. So I wrote the following query for the loop:

<?php global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'product', 'include_children' => 'false' ) );
query_posts( $args );
if(have_posts()) : while(have_posts()) : the_post(); ?>

Although 'include_children' => 'false' is included in the $args it still shows the products from the subcategories. I tried changing it for 'post_parent' => 0, and using them both at the same time, but to no avail.

Here is the code for my taxonomy:

function productcat_taxonomy() {

  $labels = array(
    'name' => _x( 'Product Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Product Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Product Categories' ),
    'all_items' => __( 'All Product Categories' ),
    'parent_item' => __( 'Parent Product Category' ),
    'parent_item_colon' => __( 'Parent Product Category:' ),
    'edit_item' => __( 'Edit Product Category' ), 
    'update_item' => __( 'Update Product Category' ),
    'add_new_item' => __( 'Add New Product Category' ),
    'new_item_name' => __( 'New Product Category Name' ),
    'menu_name' => __( 'Product Categories' ),
  );    

  register_taxonomy('product-category',array('product'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'product-category' ),
  ));

}

add_action( 'init', 'productcat_taxonomy', 0 );

Where have I gone wrong?

Solutions Collecting From Web of "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 );

You have used 'include_children' => 'false', but you haven’t told WordPress which taxonomy this is about and also not what the base category is you don’t want the children of. You would do that as follows:

array(
    'post_type' => 'product',
    'tax_query' => array( array(
        'taxonomy'         => 'product-category',
        'include_children' => 'false' 
        'terms'            => 'base-category-ID',
    ) ),
);

Instead of a fixed base-category-ID, you can off course use a variable containing the name of the first category on the current post:

$categories = get_the_category();
$category_id = $categories[0]->cat_ID;

Try this:

  1. As @gdaniel said, you are giving a string in place of boolean value.
    Replace 'include_children' => 'false' to 'include_children' => false
    (Remove quote from false)
  2. You can hook into the pre_get_posts to change the query vars before getting the posts. And when on custom taxonomy template page, include your custom query to the global query. This goes to themes functions.php

function wpse239243_exclude_child( $query ) {

    //if on frontend custom taxonomy template
    if( $query->is_tax( 'product-category' ) && $query->is_main_query() && !is_admin() ) {

        $tax_query = array( array(
            'taxonomy'         => 'product-category',
            'terms'            => $query->get( 'product-category' ),
            'include_children' => false
        ) );
        $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'wpse239243_exclude_child' );