Hide products from a Woo category, tag on the Shop Page (ok)

https://www.tychesoftwares.com/how-to-hide-a-woocommerce-product-category-on-the-shop-page/

C:\xampp\htdocs\plugindev\wp-content\themes\twentytwentytwo\functions.php

add_action( 'woocommerce_product_query', 'ts_custom_pre_get_posts_query' ); 
function ts_custom_pre_get_posts_query( $q ) {
if ( ! is_admin() && is_shop() ) {
  $tax_query = (array) $q->get( 'tax_query' );
  $tax_query[] = array(
   'taxonomy' => 'product_cat',
   'field' => 'slug',
   'terms' => array( 'party-hire'), // Don't display products in the clothing category on the shop page.
   'operator' => 'NOT IN'
  );
  $tax_query[] = array(
   'taxonomy' => 'product_tag',
   'field' => 'slug',
   'terms' => array( 'floral-hire'), // Don't display products in the clothing category on the shop page.
   'operator' => 'NOT IN'
  );
  $q->set( 'tax_query', $tax_query );
}
}

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.

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 pageif ( 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 pageif ( 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.

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

Last updated