How to hide a WooCommerce category from your shop page

How to hide a WooCommerce category from your shop page

For an e-commerce site, the most important page is the shop page where most of the business is done, and where customers can see the products for sale.

So the shop page has to be flawless; from presentation to functionality. By default, all the products from all categories will be shown which is fine for some, but not desirable for others.

You might only want to show a specific category on your shop page, but don’t know how. The following instructions created by our team at wpDataTables will show you how to hide a WooCommerce category from your shop page.

Hide the WooCommerce Category from the Shop Page

There are two different methods:

Method #1: Hiding the Category Using the get_terms Filter

It’s undesirable to show all the categories on your shop page, and so you’ll want to hide some, especially if you don’t want the generic, “uncategorized” category to show up, which makes your site more confusing and somewhat unappealing.

It can be hidden from the shop page easily, or you can rename the category and make it more relevant. You can hide it with this method.

  • First, you’ll need to find a slug for the category. Go to Products > Categories from the WordPress dashboard.
  • The slug for the Uncategorized category is “uncategorized”. If you want to make this category hidden from your child theme, enter the following code into functions.php.
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;
}


  • the get_terms function has been added to the category. The category “Uncategorized” should not be visible anymore.

  • You can then enter the slug names for all the categories you want to hide:
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 specific code will hide the items from the category “Uncategorized” and “furniture”.

Method #2: Using the “woocommerce_product_query_tax_query” filter

The second method for hiding uncategorized products from the shop page is by using the action called woocommerce_product_query. You’ll need to add this with the functions.php file. Add the code this way:

add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' );
/**
* Hide Product Categories from targeted 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 = (array) $q->get( 'tax_query' );

$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'operator' => 'NOT IN'
);

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

This code targets the pages where you don’t want the Product Categories displayed. You’ll need to set the values in the $tax_query array to the values of the categories you want to be removed.

How?

Using the “woocommerce_product_query_tax_query” filter we check whether there is already an “is_shop()” function used on the page. With the query, we can add more taxonomy search criteria to be more specific.

Then, add an array to the taxonomy that will reduce or remove the desired categories on their slugs. In the example above, we removed the T-shirts and hoodies categories. You can specify any values you want.

You can use either of these two methods to hide the WooCommerce category from the shop page.

How to Not Show WooCommerce Categories in Widgets

This script is useful for removing the category from the widget. It filters the woocommerce_product_categories_widget_args hook. Here’s the code:

add_filter( 'woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category' );
function organicweb_exclude_widget_category( $args ) {
// Enter the id of the category you want to exclude in place of '30'
$args['exclude'] = array('30' );
        return $args;
}

How to Hide Categories from Users

You can have a lot more control over who can and can’t see the categories. This is especially useful if you want to hide the categories from users. The WooCommerce hide category from shop page for users function is done like this:

add_filter( 'woocommerce_product_query_tax_query', 'hwn_hide_shop_categories_by_role');
function hwn_hide_shop_categories_by_role($tquery) {
	$user = wp_get_current_user();
	$blocked_user_roles = array("customer","administrator");
$hidden_categories = array("tshirts", "hoodies");
	if (is_shop() && (!is_user_logged_in() || is_user_logged_in() && count(array_intersect($blocked_user_roles,$user->roles)) > 0)) {
		$tquery[] =
			array(
				'taxonomy' => 'product_cat',
				'terms'    => $hidden_categories,
				'field'    => 'slug',
				'operator' => 'NOT IN'
			);
	}

	return $tquery;
}

Here’s how this code works.

In essence, this snippet of code is similar to the above code. However, this code will obtain the details of the current user with the function wp_get_current_user. Then, you can define the roles that you want the categories to be hidden from. With this code, we have defined that only the categories tshirt and hoodies will be visible for the selected users.

With this code, we’ll hide the categories.

if (is_shop() && (!is_user_logged_in() || is_user_logged_in() &&
count(array_intersect($blocked_user_roles,$user->roles)) > 0))

This code works in this way: the first part of the if statement checks whether you are on the same page as before, and identifies if the user is still logged in. If they belong to blocked users, they will not be able to see the categories. Remember to use the codes in the functions.php file of your theme.

How to Hide Products from a WooCommerce product category

In this tutorial, we’ll take a look at how to remove products from categories instead of removing categories in the first place.

Perhaps you don’t want to show the products from a specific category that you have deleted before. So now the categories “uncategorized” and “furniture” will not be shown, but how can you prevent the products from these categories from showing as well? The screenshot shows two products from the category Furniture.

You’ll need to add this code snippet to the functions.php file:

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

So after we enter this code, the products from the Furniture and the Uncategorized categories should not be visible. If you want to remove the categories from showing as well, use the code snippets displayed above.

You can remove the categories as well as the products from the given categories.

Ending thoughts on hiding the WooCommerce category from the shop page

The WooCommerce hide category from the shop page function is important to make your shop page easier to use and manage. It helps to easily organize your products, and you can categorize the products in the right categories.

It’s also great for customers who can choose their desired products, so customer satisfaction will increase, which means an increase in your traffic, sales, and conversions.

You can use wpDataTables which comes in the form of a WordPress plugin which enables you to create easy-to-use, customizable, manageable charts and tables for WordPress. It’s an easy way to control your business.

If you enjoyed reading this article about hiding the WooCommerce category from the shop page, you should read these as well:

Up Next:

How to change font color in WordPress (Quick guide)

How to change font color in WordPress (Quick guide)