I think I get what you mean. By default, when a category is clicked and you are directed to the category page, posts from the current category and all its child categories are displayed
This is how category pages are constructed by default. The main query (which also uses WP_Query
) uses a tax_query
to call posts from the db on category and taxonomy archive pages. A tax_query
on category pages and taxonomy pages are exactly the same as there are virtually no difference between the build in taxonomy category
and custom taxonomies. See this post for an explanation .
By default, if you look at a tax_query
, the include_children
parameter is set to true, which means that the child terms of the current term being queried are also included. So, you basically just need to set this parameter to false. To accomplish this, you will need to make use of the parse_tax_query
filter
You can simply do something like this in your functions.php file (Requires PHP 5.3+)
add_filter( 'parse_tax_query', function ( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_category() ) {
$query->tax_query->queries[0]['include_children'] = 0;
}
}
This will ensure that when a category is clicked, it will only show posts from that particular category, and not also from its children