Reputation: 45
I'm using this snipet to hide child categories on Woocommerce parent category page.
function exclude_product_cat_children($wp_query) {
if ( isset ( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query()) {
$wp_query->set('tax_query', array(
array (
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $wp_query->query_vars['product_cat'],
'include_children' => false
)
)
);
}
}
add_filter('pre_get_posts', 'exclude_product_cat_children')
Is there anyway to hide only a specific child category from the parent category page?
Thanks in advance!
Upvotes: 1
Views: 358
Reputation: 949
Try to use this function for your functionality. It may work...
function exclude_product_cat_children($wp_query) {
if ( isset ( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query()) {
$wp_query->set('tax_query', array(
array (
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array('cat', 'dog'), //product category slug to exclude
'operator' => 'NOT IN',
)
)
);
}
}
add_filter('pre_get_posts', 'exclude_product_cat_children');
Upvotes: 2