Skip Navigation

[Resolved] Views Custom Sorting Not Working

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by jesseB-4 6 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1123959
Screen Shot 2018-10-09 at 12.05.12 PM.png

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!

#1124323
Secondary-sorting.JPG

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' ),
#1128422

Topic moved to different thread. Closing this one for now.