Columns can be sorting by their heading using the ‘Sorting’ element.
To do this, add a sorting element to any column heading
Sorting by ‘Custom Field’
While sorting by custom field, please note the custom field type – number or text, and choose an option for ‘Sort by’ accordingly. If you select the wrong option, the sorting may malfunction in some cases.
If the custom field values are text type, you must select ‘Custom field as text’
And if the custom field values are numbers, you must select ‘Custom field as number’.
Sorting by an ‘Attribute’
(You can use this same method for sorting by category or other taxonomy)
By default WooCommerce does not allow sorting based on product attributes. So we will use another route to achieve attribute based sorting. We will sort the products based on custom field and give each product a custom field representing its attribute term.
Once you have added a new custom field to each product with the attribute term as the value, you can not sort the column by the custom field, and in effect, it will appear as if the column is being sorted by an attribute.
You can see the same system working very well for the WCPT PRO demos.
Let’s take an example: In the following screenshot from the ‘Courses’ demo, consider the ‘Subject’ column. ‘Subject’ is a WooCommerce attribute for the products in this table. However, you can see the sorting is working for this column.
Each Course product has a term set for the ‘Subject’ attribute.
Each Course product is also given a ‘Subject’ custom field with a corresponding value matching the attribute term for the product.
The custom field section can be revealed on any product editor page by using the ‘Screen Options’ tab at the top right of the page. The custom field section will appear further down on the page.
For more information on how to set custom fields, please check the WordPress documentation on custom fields.
Continuing with our example: Now in the table settings > Columns, in the heading of the Subject column, a ‘Sorting’ element is added. It is configured to sort the column by ‘Custom field as text’ when the heading is clicked. And the ‘Custom field’ is set to ‘Subject’.
And therefore, the column becomes sortable by the WC attribute ‘Subject’.