Skip Navigation

[Resolved] Multiple Sort Criteria in Views

This thread is resolved. Here is a description of the problem and solution.

Problem:
Trying to sort a view on more than one field value, and I'm only given limited options for a secondary sort.
Solution:
This feature was submitted and is under consideration still.

So right now the solution would be to do that using custom code and wpv_filter_query hook:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

You can use the code given in above ticket that you have posted. OR a solution given below that may work as well (modification could be needed):
https://toolset.com/forums/topic/order-by-two-or-multiple-parameters/#post-263775
Relevant Documentation:

This support ticket is created 7 years, 2 months 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by carlosB-2 7 years, 2 months ago.

Assisted by: Noman.

Author
Posts
#562227

Tell us what you are trying to do?

I'm trying to sort a view on more than one field value, and I'm only given limited options for a secondary sort.

I found this post from 2012: https://toolset.com/forums/topic/multiple-sort-criteria/#post-15873 that offers a method that can be added to my functions.php file, but I also found several other forum posts from around the same time saying that multiple sort criteria is in the works, and I'm just wondering if that has happened yet.

If not, are there any other methods to try for doing multiple sorts in one view?

Thanks!

#562364

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Carlos,

Thank you for contacting Toolset support. This feature was submitted and is under consideration still.

So right now the solution would be to do that using custom code and wpv_filter_query hook:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

You can use the code given in above ticket that you have posted. OR a solution given below that may work as well (modification could be needed):
https://toolset.com/forums/topic/order-by-two-or-multiple-parameters/#post-263775

Thank you

#562538

Thank you... I will use those resources to work on this.

#563763

Hi Norman - I'm having trouble adding the code using the sample code you sent me... this is what I'm using in my functions.php file.

add_filter('wpv_filter_query', 'setup_order', 10, 2);
function setup_order($q, $view) {
        // adjust this condition to decide when to apply the filter
        if ($view['view_id'] == 4072) {  
                add_filter('posts_orderby', 'adjust_order');
        }
        return $q;
}
function adjust_order($order) {
        remove_filter('posts_orderby', 'adjust_order');
        global $wpdb;
        $order .= ', ' . $wpdb->prefix . 'postmeta.wpcf-thescon-room-number ASC';
        echo $order;
        return $order;
}

The view itself is sorted on a field called thescon-session, ascending and is output here:

hidden link

I think my problem is in the adjust_order function where we're adding the second field to sort on...

$order .= ', ' . $wpdb->prefix . 'postmeta.wpcf-thescon-room-number ASC';

I've also tried posts.thescon-room-number ASC and that doesn't work either.

Actually, when I use these functions, I'm getting no results at all. If I disable the functions, I do get results.

Thanks in advance for your help.