I see that it is possible to add your custom field for sorting products in WooCommerce (e.g. this question) Copying from the example:
add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_modified_date' );
function enable_catalog_ordering_by_modified_date( $args ) {
if ( isset( $_GET['orderby'] ) ) {
if ( 'modified_date' == $_GET['orderby'] ) {
return array(
'orderby' => 'modified', //This is the custom field to be sorted by, and what I am asking for
'order' => 'DESC',
);
}
}
return $args;
}
Is there a list of all fields that I can use for sorting?
P.S. Thanks for the comment, but what I need is not sorting normal posts in WordPress, but the products in WooCommerce.
These are the default list of orderby options available( id, title, relevance, rand, date, price, popularity, rating). The case of the switch case may be what you are looking for.
switch ( $orderby ) {
case 'id':
$args['orderby'] = 'ID';
break;
case 'menu_order':
$args['orderby'] = 'menu_order title';
break;
case 'title':
$args['orderby'] = 'title';
$args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC';
break;
case 'relevance':
$args['orderby'] = 'relevance';
$args['order'] = 'DESC';
break;
case 'rand':
$args['orderby'] = 'rand'; // @codingStandardsIgnoreLine
break;
case 'date':
$args['orderby'] = 'date ID';
$args['order'] = ( 'ASC' === $order ) ? 'ASC' : 'DESC';
break;
case 'price':
$callback = 'DESC' === $order ? 'order_by_price_desc_post_clauses' : 'order_by_price_asc_post_clauses';
add_filter( 'posts_clauses', array( $this, $callback ) );
break;
case 'popularity':
add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
break;
case 'rating':
add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
break;
}
The above code is from woocommerce/includes/class-wc-query.php line 586...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With