How to exclude certain archives from the pages of a woocommerce shop?

I have on the store page displays categories of products, but I need some of them there does not show how to do it better? At the moment I use this colback, but it only works in the display mode "show the goods", and I have the show categories:

dd_action( 'pre_get_posts', 'remove_cat_from_shop_loop' );
remove_cat_from_shop_loop function( $q ) {
 if ( ! $q->is_main_query() ) return;
 if ( ! $q->is_post_type_archive() ) return;
 if ( ! is_admin() && is_shop() ) {
 $q->set( 'tax_query', array(array(
 'taxonomy' => 'product_archive',
 'field' => 'slug',
 'terms' => array( 'barhat' ), // Change it to the slug you want to hide
 'operator' => 'NOT IN'
)));
}
 remove_action( 'pre_get_posts', 'remove_cat_from_shop_loop' );
}
July 2nd 19 at 17:34
1 answer
July 2nd 19 at 17:36
Solution
Categories are displayed using the standard functions get_terms(). It can be filtered:
function woo_exclude_categories( $terms, $taxonomies, $args ) {

 // Slugs of the categories to exclude
 $exclude_categories = array(
'category-one',
'category-two',
);

 $new_terms = array();

 if ( ! is_admin() && // not admin
 is_shop() && // the store page
 in_array( 'product_cat', $taxonomies ) // this is the terms of the taxonomy 'product_cat' 
 ) {

 foreach ( $terms as $key => $term ) {

 if ( ! in_array( $term->slug, $exclude_categories ) ) {
 $new_terms[] = $term;
}

}
 $terms = $new_terms;

}

 return $terms;
}
add_filter( 'get_terms', 'woo_exclude_categories', 10, 3 );

Code in haste, you can try to filter by term_id, if you prefer, somewhere pagefactory.
Thank you, works flawlessly! But here's the thing: categories have disappeared from the admin panel now - eldora.Nolan80 commented on July 2nd 19 at 17:39
: guilty) do not protest. Changed the code in the answer, should work now standards. - Velva.Rogahn commented on July 2nd 19 at 17:42
everything worked) and why? it was the same condition, they say !is_admin(); - eldora.Nolan80 commented on July 2nd 19 at 17:45
: the array $terms the original, which transferred to the filter, I return only if you have performed the if, if was not running (just in the admin), then the filter is not returning anything. - Velva.Rogahn commented on July 2nd 19 at 17:48

Find more questions by tags WooCommerceWordPress