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. WooCommerce product sorting by 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.
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 |
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. The shortcode thus reads as [product_categories orderby=’ID’]
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.
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. The shortcode will read as [product_categories orderby=’ID’ order=’DESC’]
The categories will now be displayed in the descending order of their IDs, or, in other words, as per what category is most recent:
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:
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 ‘title’ 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; }
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.
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
Hello,
We have tested this code in 2023 and it is working for us on WooCommerce 7.8 on the Storefront theme. If possible, please let us know what problems you are facing.
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.
You can try this one: https://wordpress.org/plugins/woocommerce-product-sort-and-display/
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.