How to show term-specific post list, without posts associated with child terms?

As @Milo’s pointer said, this:

  1. Sadly, is normal WordPress behaviour.
  2. Can be suppressed using parse_tax_query and include_children set at 0

The code found in the answer at https://wordpress.stackexchange.com/a/202773/39300 Works for me…

function taxonomy_archive_exclude_children($query){
   $taxonomy_slugs = ['product_category', 'application_category'];
   if($query->is_main_query() && is_tax($taxonomy_slugs)){
      foreach($query->tax_query->queries as &$tax_query_item){
        if(empty($taxonomy_slugs) || in_array($tax_query_item['taxonomy'], $taxonomy_slugs)){
            $tax_query_item['include_children'] = 0;
        }
      }
   }
 }
 add_action('parse_tax_query', 'taxonomy_archive_exclude_children');
//  An empty $taxonomy_slugs array will exclude children for all taxonomy archives.

parse_tax_query fires at taxonomy time, include_children 0 is where I can deny children from appearing.

Leave a Comment