If the point of the question is whether there is a list that returns all the fields that can be used for sorting, the short answer is no.
The woocommerce_get_catalog_ordering_args
filter allows you to add your own ordering args, but there is no list that would store these fields. This filter is used to overwrite args, not to store it as an array.
Within the get_catalog_ordering_args()
function, some orderby
defaults are statically specified: id
, menu_order
, title
, relevance
, rand
, date
, price
, popularity
, rating
.
But there is no way to tell what values have been added using a filter by WordPress plugins.
Possible solution:
There is a woocommerce_catalog_orderby
filter that stores sorting options as an array.
This is used to display sorting options in the view. So you can assume that WordPress plugins will add options to this as well.
/**
* Add orderby option by plugin
*/
add_filter( 'woocommerce_catalog_orderby', function ( $options ) {
$options['order_by_plugin'] = __( 'Order by plugin setting', 'text-domain' );
return $options;
} );
/**
* Get orderby options
*/
$catalog_orderby_options = apply_filters( 'woocommerce_catalog_orderby', array(
'menu_order' => __( 'Default sorting (custom ordering + name)', 'woocommerce' ),
'popularity' => __( 'Popularity (sales)', 'woocommerce' ),
'rating' => __( 'Average rating', 'woocommerce' ),
'date' => __( 'Sort by most recent', 'woocommerce' ),
'price' => __( 'Sort by price (asc)', 'woocommerce' ),
'price-desc' => __( 'Sort by price (desc)', 'woocommerce' ),
) );
You can use $catalog_orderby_options
in which the sorting options are stored, if you only need the orderby fields, use the array_keys()
function.
Note: you must specify the default value in apply_filters()
, because WooCommerce added these statically, not using add_filter()
. So you have to add it statically too.