We have seen in this post how to show WooCommerce product categories on the homepage using the product_categories shortcode. However, in some cases, you may want to hide a particular category from getting displayed. Let’s explore in this post how to hide a WooCommerce category using the product_categories shortcode.
The [product_categories] shortcode has the following attributes:
Parameter | Accepted Value | Description | Default Value |
number |
12 |
Number of categories that you want to display | All categories with products |
orderby |
ID | title | name |
Order the product by | name |
order |
ASC, DESC |
Determines the order of the categories, whether ascending or descending. | ASC |
columns |
4 | Number of columns | 4 |
hide_empty |
0 | 1 |
0 to show empty categories and 1 to hide categories which don’t have any products listed under them. | 1 i.e. true |
parent |
0 | 1 |
0 to display only the parent categories and 1 to display all the categories (with sub-categories). However, upon testing, it was found that on manually assigning the value as 1 to this parameter, no categories get displayed. | 1 |
ids |
12 |
comma separated list of category IDs of categories that you want to display |
While there is no parameter or attribute to select the IDs you want to hide, with the help of the ids parameter, you can mention IDs of all categories except the one you wish to hide.
The first task is to find out the ID of the category we want to hide. There are multiple ways to do this:
One way is to hover the mouse pointer over the name of the category on this page itself (Products->Categories) and spot the category ID in the status bar below:
We can see that the product category ID for the category Furniture is 18.
There’s another way to find out Category IDs – through the database! Just open the wp_terms table of the database of your WooCommerce Store and look for the category here:
Sometimes, it is better to be able to view all IDs in one go, especially if you want to hide multiple categories & view their IDs. In this case, you can take the help of some PHP code. In the below code snippet, we can print the IDs of all categories with products in them, and copy all of them, except the ID or IDs we wish to hide. Let’s see how.
Open the functions.php file of your child theme and add the following code:
add_action( 'product_cat_pre_add_form', 'ts_print_product_cat_ids', 7 ); function ts_print_product_cat_ids() { $ids = ''; $categories = get_categories( array('taxonomy' => 'product_cat' ) ); foreach( $categories as $category ) { $ids .=$category->name.': '. $category->term_id . ', '; } echo 'Category IDs: ' . $ids; }
This will print the product category names along with their IDs on the Products->Categories page inside the admin dashboard:
We can mention the IDs we want in our shortcode, and leave out the IDs of the categories that we don’t want.
We can edit the above code snippet to make it easier for us to copy the IDs and paste them directly in the shortcode. First, make a mental note of the ID or IDs you don’t want.
add_action( 'product_cat_pre_add_form', 'ts_print_product_cat_ids', 7 ); function ts_print_product_cat_ids() { $ids = ''; $categories = get_categories( array('taxonomy' => 'product_cat' ) ); foreach( $categories as $category ) { $ids .=$category->term_id . ', '; } echo 'Category IDs: ' . $ids; }
This edited code snippet above will print only all the category IDs so that you can copy them directly to paste them inside the shortcode (after erasing the one you don’t want):
In our case, we want to erase one category which is “Furniture” and we have now seen three different ways to find out the ID of this category. It is 18. Now, let’s try to display all categories except this one, for the purpose of this tutorial.
We can copy all the categories IDs that we printed using the PHP code on the Products->Categories page and paste them inside the shortcode [product_categories] such that the shortcode now becomes [product_categories ids=”15, 19, 20, 16, 17″]. We have erased the ID 18 from here. Let’s add this code to our homepage:
Once you click on Update and visit the homepage, you can see that all categories with products are displayed, except for the Furniture Category.
You can also now erase the code snippet from the functions.php file as it was only for the purpose of printing the IDs somewhere for your reference.
There you go! In just a few simple steps, it is possible to hide a category from displaying using the [product_categories] shortcode.
This helped me, thanks
You’re welcome!!