Hide category in the WooCommerce shop page

php, woocommerce

Solution

I know this is a bit late, but had this problem myself and solved it with the following function:

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

function get_subcategory_terms( $terms, $taxonomies, $args ) {

  $new_terms = array();

  // if a product category and on the shop page
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {

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

      if ( ! in_array( $term->slug, array( '**CATEGORY-HERE**' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;
  }

  return $terms;
}

Problem

I've been trying to hide a specific category from SHOP page. I found this code: ``` add_filter( 'pre_get_posts', 'custom_pre_get_posts_query' ); function custom_pre_get_posts_query( $q ) { if ( ! $q->is_main_query() ) return; if ( ! $q->is_post_type_archive() ) return; $q->set( 'tax_query', array(array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array( 'CATEGORY TO HIDE' ), 'operator' => 'NOT IN' ))); remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' ); } ``` I've pasted this code in my theme function.php file but I'm not achieving the result... Can anybody help me please?

Original source