Hello,
Last I reached out to Toolset, we were trying to add custom secondary sorting to the Toolset Views to help us output orders from orderpantry.com in a chronological order using Order Delivery Date Pro's time slots.
We've figured out how to add custom sorting using the time slots and added it to the functions.php of our staging website at staging1.orderpantry.com. Here is our code below:
add_filter('wpv_filter_query', 'custom_order_due_sorting', 99, 3);
function custom_order_due_sorting($query_args, $view_settings, $view_id ) {
if ( $view_id != 6041 ){
return;
}
$meta_query = array();
$meta_query[] = array('key' => 'Order Due Time');
if ( !isset($query_args['meta_query']) ){
$query_args['meta_query'] = array();
}
$query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
add_filter('posts_orderby', 'kds_custom_order');
return $query_args;
}
function kds_custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value ASC';
}
This is working to some degree, but not fully. We still get orders sorted by their ID instead of their time slots and we're not really sure why. For example, please see the screenshot I attached: Order 6660 is due on Oct. 9 11:30 AM - 12:00 PM and Order 6659 is due on 1:00 PM - 1:30 PM. 6660 should be ahead of 6659.
Hoping someone at Toolset can tell us what went wrong and why our code don't work. Thank you!
Hello,
In the latest version of Views plugin, there is a built-in feature to setup the "Secondary sorting", see screenshot Secondary-sorting.JPG
And you can follow wordpress document to setup your orderby parameters:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
section "Mulitiple orderby/order pairs":
'orderby' => array( 'meta_value_num' => 'DESC', 'title' => 'ASC' ),
Topic moved to different thread. Closing this one for now.