Search the site:

How to sort WooCommerce categories

WooCommerce product categories that get displayed on the Shop page or on the Front page (if you choose to display them there) follow a default ascending order and are ordered by their names. Sometimes, you may want to change this order. For example, ordering product categories in descending order of their IDs will help customers see the latest categories of products that are added by you whenever they visit your store. Let’s dive into this post to learn how to sort WooCommerce categories using both a shortcode and a code snippet.

1. Sorting WooCommerce product categories using a shortcode

WooCommerce provides a lot of shortcodes to be used for displaying products and product categories. As seen in this post, the [product_categories] shortcode can be used to display categories on any page. It has a list of parameters, two of which we will use today to sort categories.

ParameterAccepted ValueDescriptionDefault Value
number12Number of categories that you want to displayAll categories with products
orderbyID | title | nameOrder the product byname
orderASC, DESCDetermines the order of the categories, whether ascending or descending.ASC
columns4Number of columns4
hide_empty0 | 10 to show empty categories and 1 to hide categories which don’t have any products listed under them.1 i.e. true
parent0 | 10 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
ids12comma separated list of category IDs of categories that you want to display

The order_by parameter can be assigned different values depending on what we wish to order the categories by. They are already ordered by their names by default, let’s try assigning the “ID” value to this parameter.

How to sort WooCommerce categories - Tyche Softwares
How to sort WooCommerce categories – Shortcode to order categories according to their IDs.

Similarly, the order parameter has the value “ASC” by default which means that the categories are displayed in ascending order. You can see that the categories are displayed in ascending order of their IDs as we have assigned the value of “ID” to the orderby parameter.

How to sort WooCommerce categories - Tyche Softwares
How to sort WooCommerce categories – Categories ordered in default ascending order of their IDs on the home page.

As “Uncategorised” is an in-built WordPress category, its ID will always be lower in number than any categories added after it.

If you want your customer to see the categories in order of what’s recently added, you would need to change the value of the order parameter from ASC to DESC.

How to sort WooCommerce categories - Tyche Softwares
How to sort WooCommerce categories – Shortcode to order categories in the descending order of their IDs.

The categories will now be displayed in the descending order of their IDs, or, in other words, as per what category is most recent:

How to sort WooCommerce categories - Tyche Softwares
How to sort WooCommerce categories – Categories ordered in descending order of their IDs on the home page.

2. Sorting WooCommerce product categories using a code snippet (Shop Page)

If you have used the options available under “Customize” to display categories on the Shop page, the code snippet below can be used to change the order of those categories. This code should be added to the functions.php file of your child theme.

add_filter( 'woocommerce_product_subcategories_args', 'ts_woocommerce_get_subcategories_ordering_args' );

function ts_woocommerce_get_subcategories_ordering_args( $args ) {
         $args['order'] = 'desc';
         $args['orderby'] = 'title';
         return $args;
    }

This will display the categories in the descending order of their titles (names) on the Shop page:

How to sort WooCommerce categories - Tyche Softwares
How to sort WooCommerce categories – Categories ordered in descending order of their titles or names on the Shop page.

Similarly, if you wish to sort these categories in the descending order of their IDs (to show latest categories first), the value assigned to the orderby parameter will change from ‘name’ to ‘ID’ in the code snippet:

add_filter( 'woocommerce_product_subcategories_args', 'ts_woocommerce_get_subcategories_ordering_args' ); 

function ts_woocommerce_get_subcategories_ordering_args( $args ) { 
          $args['order'] = 'desc'; 
          $args['orderby'] = 'ID'; 
          return $args; 
}
How to sort WooCommerce categories - Tyche Softwares
How to sort WooCommerce categories – Categories ordered in descending order of their IDs on the Shop page.

The values “name” and “title” both order the categories in the order of their names. In this way, you can sort categories on the homepage (or any page) as well as the Shop Page using a shortcode and code snippet respectively.

You can also check this article for a complete guide to generate WooCommerce Product Feed.

Browse more in: WooCommerce How Tos, Code Snippets, WooCommerce Tutorials
Subscribe
Notify of
guest

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

3 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Ivan
Ivan
2 years ago

wondering if i can use this to sort products with dokan. Since there is no just one shop page but every vendor have their own shop page.

Alice
1 year ago

Thanks for your post. Can you recommend a plugin that supports sorting WooCommerce categories? To be honest, I’m not really familiar with shortcode or coding.

Ashish Kumar
1 year ago

Hello Sir
Above code snippet is not working now and showing errors, i has used this code before that time it was working but now showing errors. please update it.
Thank You

from purchased

Subscribe for more offers

Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins and 50% OFF on Bundles
3
0
Would love your thoughts, please comment.x
()
x