ThemeIM Blog Details

How to Exclude Products from a Particular Category on the WooCommerce Shop Page

How to Exclude Products from a Particular Category on the WooCommerce Shop Page

Sometimes it may happen that your store page is not created properly for a particular category of product, which you do not want to show your customers at the moment. In that case, you might want to exclude or hide that particular category of product from the WooCommerce shop page. But alas, you can’t find a way.

— In today’s article, we will show you how to exclude or hide a certain category of products from the WooCommerce shop page. The work can be done through very simple code. So, read the whole article very carefully.

Although there are plugins that let you hide products and categories for WooCommerce, who needs them when you can do this with a simple code snippet.

But first, you will need to know –

How to access the functions.php file

There are 2 methods of accessing the functions.php file. They are –

  • Using the WordPress admin interface, and
  • Through the account control panel

Accessing the functions.php file using the WordPress admin interface is easier and this is how most people access this file. So, without further ado, we will show you how to access the functions.php file from the WordPress admin interface –

1.  First login to the WordPress interface by going to –

www.domain.com/wp-admin

2. Now, click on the Theme Editor by hovering over to Appearances

Theme Editor Section

3. You will find the php file from the right sidebar

Appearance - Theme editor

Easy right? Now that you have learned how to access the functions.php file, we will show you how to exclude products from a particular category on the shop page.

Exclude products from a particular category on the shop page

Before going any further, we must remind you this will only work when –  ‘Show Products’ is enabled under the “Shop Page Display” option. This option can be found by going to the following settings –

WooCommerce > Product catalog    

Adding the following code to the functions.php file will let you exclude products from a particular category on your WooCommerce shop page. We advise you not to add custom codes directly to the parent theme’s function.php file. Create a child theme first, then add the following code to the child theme’s functions.php file –

/**
* Exclude products from a particular category on the shop page
*/
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'clothing' ), // Don't display products in the clothing category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}

Please note:

Here ‘clothing’ is used just for an example. Be sure to use a particular product category slug that really exists in your WooCommerce site.

How to hide WooCommerce product category on the Shop Page

If you just want to hide a particular product category from the WooCommerce shop page, you can use the slug of that product. In order to do that, you need to go to –

WooCommerce>Products>Categories

For instance, we will be using the ‘uncategorized’ slug for ‘Uncategorized’ products. Set the slag of the product you want to hide. Now, select the child theme’s function.php file from –

Appearance>Theme Editor

Add the following code in the file –

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;
}

Similarly, multiple slugs can be used with commas to hide them at the same time. Use the following code as a reference -The get_term filter for ‘uncategorised’ slug will hide the products from the WooCommerce shop page that are under the ‘Uncategorized’ category.

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','cat' ) ) ) { //pass the slug name here
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}

As you can see, we have used both ‘uncategorised’ and ‘cat’ slugs to hide them from the WooCommerce shop page simultaneously. Use your preferred slugs depending on the category of products.

Related One: How to Remove Add to Cart Button from WooCommerce

Conclusion

Hurray! Now you know how to exclude or hide a product category from the WooCommerce shop page. After reading this article you may be surprised that you did not know this simple task for so long. You may have installed a number of plugins to exclude or hide products from your shop page, most of which did not work very well.

— We are assuming this article was quite helpful for you, thanks for your time, leave a comment if you have any confusion. We will see you at the next insightful blog like this one.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.