Hide Subcategory Posts From Parent Categories
Makes a category archive list only the posts assigned directly to that category, not the posts from its child categories.
Opens WPIDE on
Code
add_action( 'parse_tax_query', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_category() || empty( $query->tax_query->queries ) ) {
return;
}
foreach ( $query->tax_query->queries as $index => $clause ) {
if ( is_array( $clause ) && isset( $clause['taxonomy'] ) && 'category' === $clause['taxonomy'] ) {
$query->tax_query->queries[ $index ]['include_children'] = false;
}
}
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.