Home » WooCommerce: Exclude Category from [product_categories] Shortcode

WooCommerce: Exclude Category from [product_categories] Shortcode

by Tutor Aspire

Sometimes solutions are very simple, and you don’t need rocket science to fix your issue!

A client of mine needed to hide a category from the Product Categories Shortcode (“product_categories”); in fact, there is no parameter that allows you to “exclude” a given product category such as “uncategorized” or whatever category you wish.

A rare issue, but once again a quick fix! Here’s how you do it.

1. Find WooCommerce Category IDs

First of all you need a list of all the WooCommerce category IDs in your store. To do so you can proceed in two ways.

The first is purely manual. This is good if you have no more than 20 categories. Go to the Product Categories page, hover on each category “Edit” link and note down the ID which is contained within the URL:

Find WooCommerce category IDs

The second method is a little smarter 🙂 You can use PHP in the admin to print a list of category IDs, so you don’t need to worry about doing that manually.

You can use this snippet to print a list of all Category IDs:

/**
 * @snippet       Print List of Category IDs @ Product Categories Admin
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */
 
add_action( 'product_cat_pre_add_form', 'tutoraspire_list_all_product_cat_ids', 5 );
 
function tutoraspire_list_all_product_cat_ids() {
   $ids = '';
   $categories = get_categories( array( 'taxonomy' => 'product_cat', 'fields' => 'ids' ) );
   echo 'Category IDs: ' . implode( ', ', $categories );
}

Here’s the result:

Get a list of Product Category IDs

2. Exclude the Category IDs from the “product_categories” Shortcode

Now that you have all IDs and the ID of the category you want to hide, simply exclude that from the list of ids inside the shortcode!

// -----------------------
// #1 Show all categories
 
[product_categories]
 
// -----------------------
// #2 Show all categories but ID = 8
// ## All category ids: 8, 15, 34, 20, 18, 37, 10
// ## Include all but "8":
 
[product_categories ids="15, 34, 20, 18, 37, 10"]

You may also like