Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to hide a WooCommerce product category on the Shop Page

We have seen how you can hide a product category from getting displayed using the [product_categories] shortcode. But what if you want to hide a product category on the Shop Page, after displaying it using the WordPress Customizer? In this post, you will learn how you can hide a WooCommerce product category on the Shop Page, as well as hide products from a particular category.

An easy way to display product categories or products on the WooCommerce Shop Page is by using the Customizer which you can access by clicking on the Customize option on the top left while on the Shop Page, and choosing WooCommerce->Product Catalog:

How to hide a WooCommerce product category on the Shop Page - WooCommerce Product Catalog

Inside the Product Catalog option, you can choose what to display on the Shop Page, whether it’s Products , Product Categories or both Product Categories & Products. For the purpose of this tutorial, we will choose “Show categories & products”.

How to hide a WooCommerce product category on the Shop Page - How to display Categories & Products on the Shop Page

 

Hiding a WooCommerce product category on the Shop Page

Suppose you don’t want to show a particular category. For the purpose of this tutorial, let us consider hiding the Uncategorised category. We will start with finding a slug for this category. You can do this by navigating to Products->Categories in your WordPress admin dashboard.

How to hide a WooCommerce product category on the Shop Page - Finding category slugs

We can see that the slug for the Uncategorised category is ‘uncategorised’ itself.

Add the below code snippet to the functions.php  file of your child theme in order to make this category hidden.

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

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

          $new_terms = array();

          // if it is 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( 'uncategorised' ) ) ) {        //pass the slug name here
                    $new_terms[] = $term;
                 }
          }
          $terms = $new_terms;
    }

    return $terms;
}

We have added a function to the get_terms filter. You can now see that the category “Uncategorised” is no longer visible as it was earlier.

How to hide a WooCommerce product category on the Shop Page - Category Uncategorised hidden on Shop Page

You can insert slug names of all the categories you want to hide, separated by a comma:

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

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

          $new_terms = array();

          // if it is 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( 'uncategorised','furniture' ) ) ) {        //pass the slug name here
                    $new_terms[] = $term;
                 }
          }
          $terms = $new_terms;
    }

    return $terms;
}

This code snippet will hide categories with slugs ‘uncategorised’ and ‘furniture’:

How to hide a WooCommerce product category on the Shop Page - Categories UnCategorised and Furniture hidden on Shop Page

Hiding products from a WooCommerce product category on the Shop Page

If you have chosen to show both Products & Product Categories like we have in this example, you would see the products right after the Categories have been displayed.

How to hide a WooCommerce product category on the Shop Page - Categories and Products both displayed on the Shop Page

Suppose you don’t want to show the products from a particular category after hiding the category as well. In our case, we have hidden the ‘uncategorised’ and the ‘furniture’ categories, so let us also hide products from these categories from getting displayed. The screenshot above shows two products i.e. Balcony Lounger & Bar Stool from the category Furniture.

Let’s add the code snippet below to the functions.php file of the child theme:

add_action( 'woocommerce_product_query', 'ts_custom_pre_get_posts_query' ); 

function ts_custom_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'uncategorised','furniture'), // Don't display products in the clothing category on the shop page.
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_query );

}

The products in the categories Furniture as well as Uncategorised are no longer visible. To also hide the categories from getting displayed, you can use the code snippet mentioned earlier in this post, along with this one.

How to hide a WooCommerce product category on the Shop Page - Products from certain categories hidden on the Shop Page

In this way, you can use code snippets to hide both categories and products from categories, on the Shop Page in WooCommerce.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
11 Comments
Newest
Oldest
Inline Feedbacks
View all comments
kaushik nakrani
3 years ago

Hello, woocommerce Hide category and product and use this pluginhttps://wordpress.org/plugins/hide-categories-or-products-on-shop-page

3 years ago

Hi. the code helped to hide the ‘uncategroised’ on the ‘Shop’ page only. but it (uncategorized) still appears in the separate product category pages (not shop). How to hide ‘uncategorised’ from all pages (other than shop). Please help out… Thanks

kaushik nakrani
3 years ago
Reply to  Zeerik
Alvaro Cercos
3 years ago

Hi i have try to hide a Plan slug with this code:

add_filter( ‘get_terms’, ‘ts_get_subcategory_terms’, 10, 3 );

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

$new_terms = array();

// if it is 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( ‘plan’ ) ) ) { //pass the slug name here
$new_terms[] = $term;
}
}
$terms = $new_terms;
}

return $terms;
}

Any help would be nice

Alvaro Cercos
3 years ago

Hi, i have try to do the instructions with the function.php code editing and it haven´t solve the problem. The Slug is plan and the code I have used is the one add_filter( ‘get_terms’, ‘ts_get_subcategory_terms’, 10, 3 ); function ts_get_subcategory_terms( $terms, $taxonomies, $args ) { $new_terms = array(); // if it is 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( ‘plan’ ) ) ) { //pass the slug name here $new_terms[] =… Read more »

kaushik
3 years ago
Reply to  Alvaro Cercos
Tim
4 years ago

Hello Anubha! Thanks so much for taking the time on this. I was ALMOST able to get what I was trying to do, but found the functions code ALSO hides products on the category pages themselves too. So for example, I have a category of overstocked items that are selling for 50% of the normal cost. I don’t want this category to show on the main shop page. The code you listed certainly does that, however when I head over to the category url itself, items also do not show there either! Am I missing something perhaps? Thank you!

Brenna
3 years ago
Reply to  Tim

Tim – this is the same issue I’m running into, did you ever get this resolved on your end? What was the solution?

Tim
3 years ago
Reply to  Brenna

Hi there Brenna, I had to look it up as it was so long ago now I forgot I even asked the question haha. This was my solution, taken from another site I came across. This will go within the theme functions child. One thing to note, where it says ‘overstock’ is where you’d place the category you’d like to hide. Hope it helps!! : add_action( ‘woocommerce_product_query’, ‘prefix_custom_pre_get_posts_query’ ); /** * Hide Product Cateories from targetted pages in WooCommerce * @link https://gist.github.com/stuartduff/bd149e81d80291a16d4d3968e68eb9f8#file-wc-exclude-product-category-from-shop-page-php * */ function prefix_custom_pre_get_posts_query( $q ) { if( is_shop() || is_page(‘awards’) ) { // set conditions here $tax_query… Read more »

11
0
Would love your thoughts, please comment.x
()
x