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:
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”.
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.
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.
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’:
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.
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.
In this way, you can use code snippets to hide both categories and products from categories, on the Shop Page in WooCommerce.
Hello, woocommerce Hide category and product and use this pluginhttps://wordpress.org/plugins/hide-categories-or-products-on-shop-page
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
Would you please use this plugin.https://wordpress.org/plugins/hide-categories-or-products-on-shop-page/
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
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 »
You can use this plugin https://wordpress.org/plugins/hide-categories-or-products-on-shop-page/
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!
Tim – this is the same issue I’m running into, did you ever get this resolved on your end? What was the solution?
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 »