I am trying to find a way to hide a specific post category from all pages but one (and its child-/ sub-pages).
Let's say I have a page called "Fruits" and it's child-/ sub-pages are "Berries" and "Stone Fruits". Furthermore, I have created a post category "Fruits-Category" and some posts tagged to it. I want these posts just to be shown on the "Fruits", "Berries" and "Stone Fruits" page. Not on the home page, any archives or sidebars or any other pages. When googling, I just find examples for hiding categories on either the home page or the blog page.
Here is my code so far in functions.php
:
function tpa_exclude_technology_cat( $query ) {
if ( !$query->get_queried_object_id('8') ) { // "Fruits"-page has id of 8
$query->set( 'cat', '-3' ); // "Fruits-Category" has id of 3
}
return $query;
}
add_filter( 'pre_get_posts', 'tpa_exclude_technology_cat' );
I am not sure how to get the child-/ sub-pages of "Fruits". Maybe there is a better way to solve this?
Thanks in advance!