1. Home
  2. Documentation
  3. WooCommerce Product Table
  4. Product Table Shortcode Options
  5. Sorting & ordering the product table

Sorting & ordering the product table

WooCommerce Product Table comes with 2 options for sorting the table when it first loads. Once the table has loaded, customers can click on a column heading to sort by that column.

Sort by

The sort_by option controls which column the table is sorted by when first displayed.

If not specified, the WooCommerce default product ordering is used. This can be found under the WooCommerce > Settings > Products > Display menu.

Sort options

You can order by any of the following:

  • name – sort by product name
  • menu_order – sort by the order listed in the Products menu when in the “Sorting” view
  • id – sort by product ID
  • sku – sort by product SKU code
  • price – sort by price
  • popularity – sort by number of sales (note: see here for a tutorial on how to list popular/bestselling products)
  • reviews – sort by the average review score
  • date – sort by date published (note: see here to sort by custom field date columns)
  • modified – sort by the last modified date
  • rand – apply random ordering

In addition, if you are not using the lazy load option, you can sort by any other column present in the table. E.g. sort_by="description".

For lazy loaded tables, you can only sort by the keywords listed above regardless of which columns are present in the table. This is because the sorting is handled by the server when the products are retrieved from the database.

How to set the default sorting

You can set this option globally on the plugin settings page, or directly in the shortcode for individual tables.

Examples:

  • Sort using keyword:  [product_table columns="name,tags,categories,price,add-to-cart" sort_by="rating"]
  • Sort using any column in table (when lazy load is disabled):  [product_table columns="sku,image,name,price,add-to-cart" sort_by="sku"]

Sort order

The sort_order option controls the direction in which the table is sorted. Use asc for ascending order or desc for descending order.

The defaults is desc for date sorting and asc for everything else.

You can set this option globally on the plugin settings page, or directly in the shortcode for individual tables.

Example:

  • [product_table columns="name,price,add-to-cart" sort_by="price" sort_order="desc"]

Can I sort by multiple columns?

No, this is not currently possible. The table can only be sorted by a single column.

Can I sort the table by number?

By default, the table is sorted alphabetically rather than numerically. This applies to all text-based columns including custom fields and taxonomies.

If you’re using custom fields or taxonomies to store numbers instead of text, then you may wish to sort the table in number order. There is no option for this, but you can achieve this by adding the numbers in a format that will make sense when ordered alphabetically. For example:

  • If you add numbers in the format 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and 11, then they will be sorted in the following order – 1, 10, 11, 2, 3, 4, etc. This does not look correct.
  • If you’re sorting by a custom field containing numbers 01, 02, 03, 04, 05, 06, 07, 08, 09, 10 and 11, then they will be sorted correctly both alphabetically and numerically.

How to change the order of the items in the filter dropdowns

Most of the filter dropdowns that you can add above the product table can only be sorted alphabetically. However, it is possible to control the order in which attributes are listed. This is done in WooCommerce rather than our plugin.

WooCommerce attributes list orderThe attributes in the filter dropdowns are sorted according to the ‘Default sort order’ set in Products > Attributes. If you hover over the Name column and click the Edit link, then you will see a page where you can set the ordering.

In the example screenshot, we’ve set the “Brand” attribute to be ordered by ‘Name’ (i.e. alphabetical). ​

If you use ‘Custom ordering’ then you can drag and drop the attributes into the correct order when you edit the ​terms for that attribute (click ‘Configure Terms’ from the Products > Attributes page).

Was this article helpful?

Related Articles