Hide Specific Product Category in WooCommerce

I don’t mind admitting I’ve avoided WordPress like the plague for years. Mostly because, as a very junior developer, I was asked to fix a site which had been compromised. It was an absolute mess and it put me off using WordPress for for the following decade.

But, with new clients come new responsibilities. And, in this case, a bunch of WordPress and WooCommerce websites. So I’m learning a few things, which I guess is the upside of adopting a new platform.

Here’s a quick snippet which you can add to your theme’s functions.php page. This will allow you to hide a specific product category. Just swap [category-slug] for your product category slug and it’ll be removed from the default category list.

add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 );
function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {
  $new_terms = array();
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() &&is_shop() ) {
      foreach( $terms as $key => $term ) {
          if ( !in_array( $term->slug, array( '[category-slug]' ) ) ) {
              $new_terms[] = $term;
          }
      }
      $terms = $new_terms;
  }
  return $terms;
}
Jack Barber, freelance web developer based in Whitby, UK

Written By

I'm a freelance web developer based in Whitby, UK. I built my first website using GeoCities, and learned to write HTML and CSS using Notepad. Web technology has come a long way since then, as have my web development skills!

These days I love helping my clients make the most of the internet. I provide design, development, marketing and IT support services, forming long-term partnerships with my clients.

Connect With Me

Posted in WooCommerce, WordpressTagged ,