If you’ve ever delved into the WP core you’ll find some little items that are quite unknown. Take tax_query for example; most developers are aware of the standard options: ‘taxonomy’ ‘field’ and ‘terms’ but did you know there is an ‘include_children’ option? This returns only direct parent matches for the taxonomy terms specified – in other words, no child terms if you are using hierarchies. This is pretty straightforward for custom queries, but what about archive pages? Here’s a filter for that:
// Custom Taxonomy Page Filter
add_action('pre_get_posts', 'my_taxonomy_children_filter' );
function my_taxonomy_children_filter( $query ) {
global $wp_query;
if ($query->is_tax) {
$modded_tax_query = $query->tax_query;
$filter_tax = $modded_tax_query->queries[0]['taxonomy'];
$filter_terms = $modded_tax_query->queries[0]['terms'];
$filter_field = $modded_tax_query->queries[0]['field'];
$query->set('tax_query', array(array(
'taxonomy' => $filter_tax,
'field' => $filter_field,
'terms' => $filter_terms,
'include_children' => false
))
);
$query->parse_query();
}
return $query;
}
Works pretty well for me. Pop that into your functions.php and it should do the trick
Let me know if it doesn’t for you!