Search the site:

Home Blog How to hide a WooCommerce category using the product_categories shortcode

How to hide a WooCommerce category using the product_categories shortcode

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. This need not be done manually. With the help of some PHP code, we can print the IDs of all categories with products in them, and copy all of them, except the ID 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:

How to hide a WooCommerce category using the product_categories shortcode - Printing Product Category Names with IDs

We can mention the IDs we want in our shortcode, and leave out the ID of the category 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 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 will print only the IDs so that you can copy them directly to paste them inside the shortcode (after erasing the one you don’t want):

How to hide a WooCommerce category using the product_categories shortcode - Printing Product Category IDs

There are two more simple ways to find the IDs of the categories you don’t want. 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:

How to hide a WooCommerce category using the product_categories shortcode - Finding Product Category ID in the Status Bar
ID in the Status Bar when the mouse is hovered over the category “Furniture”

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:

How to hide a WooCommerce category using the product_categories shortcode - Finding Category in Database

Now that we are sure that the ID for the Product Category “Furniture” is 18, 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:

How to hide a WooCommerce category using the product_categories shortcode - Editing the 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.

How to hide a WooCommerce category using the product_categories shortcode - Homepage Screenshot of Categories

There you go! In just a few simple steps, it is possible to hide a category from displaying using the [product_categories] shortcode.

Browse more in: WooCommerce, WooCommerce How Tos, WooCommerce Tutorials

Leave a Reply

avatar

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

  Subscribe  
Notify of
from purchased

Subscribe Now