Search the site:

Home Blog How to sort WooCommerce categories

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.

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.

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 – 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 – 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 – 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 – 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 – 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.

Browse more in: Code Snippets, 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