Home › Toolset Professional Support › [Resolved] Sorting a view on multiple custom fields.
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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
This topic contains 10 replies, has 2 voices.
Last updated by brettB-4 1 year, 7 months ago.
Assisted by: Minesh.
I have looked through several previously filed support tickets from other users and I haven't found a solution that meets our situation. We need to be able to sort a view based on multiple custom fields.
The page we are working on is here:
hidden link
We currently have the results sorted by Date and you previously helped us group all results for each date under one date header with this support ticket:
https://toolset.com/forums/topic/group-view-results-by-day/
We now have new fields added for Session and Round. There are 8 different Sessions and 3 different Rounds. We need to do secondary sorts on the custom Session field and custom Round field. I'm looking at the View settings under Ordering and there is no way to set up secondary sorts on custom fields. Ideally our output would group all results for each session the same way we have the grouping working for the date and then for each round the same way.
May 13, 2023
Afternoon 2.5
Round 1
Result 1
Result 2
Round 2
Result 1
Result 2
Round 3
Result 1
Result 2
Afternoon 3.0
Round 1
Result 1
Result 2
Round 2
Result 1
Result 2
Round 3
Result 1
Result 2
Afternoon 3.5
Round 1
Result 1
Result 2
Round 2
Result 1
Result 2
Round 3
Result 1
Result 2
Afternoon 4.0+
Round 1
Result 1
Result 2
Round 2
Result 1
Result 2
Round 3
Result 1
Result 2
Morning 2.5
Round 1
Result 1
Result 2
Round 2
Result 1
Result 2
Round 3
Result 1
Result 2
Morning 3.0
Round 1
Result 1
Result 2
Round 2
Result 1
Result 2
Round 3
Result 1
Result 2
Morning 3.5
Round 1
Result 1
Result 2
Round 2
Result 1
Result 2
Round 3
Result 1
Result 2
Morning 4.0+
Round 1
Result 1
Result 2
Round 2
Result 1
Result 2
Round 3
Result 1
Result 2
May 12, 2023
Results sorted and grouped the same as above.
Is this possible? I'm guessing we could accomplish the grouping on each Session and each Round the same way we did it with the date field. But I'm not sure how to accomplish the alphabetical sorts on the Session and Round fields.
Hello. Thank you for contacting the Toolset support.
There is no native way to customize it and group it using the multiple custom fields. But first of all, I would like to know how exactly the field "Session"? Is it a dropdown field?
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Note that I would also definitely make it feature request for the plugin to add more levels of sorting capability to Views.
Can you please check now: hidden link
I've added the following code to the "Custom Code" section offered by Toolset with the code snippet "toolset-custom-code":
=> hidden link
add_filter('wpv_filter_query','func_sort_by_multiple_custom_fields', 199, 3); function func_sort_by_multiple_custom_fields($query_args, $view_settings, $view_id ) { $view_ids = array( 841 ); if( in_array( $view_id, $view_ids)) { $query_args['meta_query'] = array( 'relation' => 'AND', 'session_clause' => array( 'key' => 'wpcf-session2', 'compare' => 'EXISTS', ), 'round_clause' => array( 'key' => 'wpcf-round', 'compare' => 'EXISTS', ), ); $query_args['orderby'] = array( 'session_clause' => 'ASC', 'round_clause' => 'ASC', ); } return $query_args; } add_shortcode('heading', 'my_heading'); function my_heading($atts, $content = '') { static $year = null; static $month = null; static $session = null; static $round = null; $condition = $atts['condition'];; $value = $atts['value'];; switch ($condition) { case 'year': case 'month': case 'session': case 'round': if ($$condition != $value) { $$condition = $value; return $content; } break; } return ''; }
I've also removed the custom shortcode 'heading' from your theme's functions.php file and added and modified it and added to the same code snippet as you can see above.
Can you please confirm it works as expected now.
I changed the name of your custom code to
sorting-by-date-session-round-on-results-page
Here is how the results page is displaying now:
hidden link
I will have to add more Match results to know for sure how everything is working with this, but I can tell you that the dates are out of order with this setup. I hope my renaming the code snippet didn't break it. I think the sessions and rounds might be right though, but I'll have to add 10 or so more matches into the system to be sure of that. I'll go ahead and do that later tonight and see if you can get the primary sort on the date back to showing in descending order.
I've added all the additional match results. Note that this tournament was a one-day tournament, so the date grouping/sorting isn't as important for this one. But we will have multi-day tournaments in the future, so I want to make sure to get the date field as the first grouping/sorting. All the real results on the Results page now are on May 13. Everything else was just test matches I previously entered to try to get the functionality working. I've left the test Matches in the system for purposes of your work here, but as soon as this is working perfectly, I'll be deleting all those test Matches.
Note also that I had to add one additional field called Game Number. The key field in your code should probably be wpcf-game-number for that. Can we add a final sort on that after the Session sort and Round sort? Or can you show me how/where to add that myself within the code you provided? The game numbers should appear in numerical order under all Rounds. It is already sorted that way for the most part on the Results page by virtue of the order that I created the new Matches.
hidden link
But if you scroll down on that page to May 13, 2023 Morning – 3.0 Round 2, you'll see that the display order is Game 4, Game 6, Game 5.
Looking at your code, I'm thinking that it would change to this:
add_filter('wpv_filter_query','func_sort_by_multiple_custom_fields', 199, 3);
function func_sort_by_multiple_custom_fields($query_args, $view_settings, $view_id ) {
$view_ids = array( 841 );
if( in_array( $view_id, $view_ids)) {
$query_args['meta_query'] = array(
'relation' => 'AND',
'session_clause' => array(
'key' => 'wpcf-session2',
'compare' => 'EXISTS',
),
'round_clause' => array(
'key' => 'wpcf-round',
'compare' => 'EXISTS',
),
),
'game_number_clause' => array(
'key' => 'wpcf-game-number',
'compare' => 'EXISTS',
),
);
$query_args['orderby'] = array(
'session_clause' => 'ASC',
'round_clause' => 'ASC',
'game_number_clause' => 'ASC',
);
}
return $query_args;
}
add_shortcode('heading', 'my_heading');
function my_heading($atts, $content = '') {
static $year = null;
static $month = null;
static $session = null;
static $round = null;
static $game-number = null;
$condition = $atts['condition'];;
$value = $atts['value'];;
switch ($condition) {
case 'year':
case 'month':
case 'session':
case 'round':
case 'game-number':
if ($$condition != $value) {
$$condition = $value;
return $content;
}
break;
}
return '';
}
Did I do this correctly? Or is something else needed to add that additional field sort/group?
Can you please check now: hidden link
I've adjusted the code added to "Custom Code" section with the code snippet "toolset-custom-code" as given under:
=> hidden link
add_filter('wpv_filter_query','func_sort_by_multiple_custom_fields', 199, 3); function func_sort_by_multiple_custom_fields($query_args, $view_settings, $view_id ) { $view_ids = array( 841 ); if( in_array( $view_id, $view_ids)) { $query_args['meta_query'] = array( 'relation' => 'AND', 'match_day_clause' => array( 'key' => 'wpcf-match-date-and-time', 'type'=>'NUMERIC', 'compare' => 'EXISTS', ), 'session_clause' => array( 'key' => 'wpcf-session2', 'compare' => 'EXISTS', ), 'round_clause' => array( 'key' => 'wpcf-round', 'compare' => 'EXISTS', ), ); $query_args['orderby'] = array( 'match_day_clause'=>'ASC', 'session_clause' => 'ASC', 'round_clause' => 'ASC', ); } return $query_args; } add_shortcode('heading', 'my_heading'); function my_heading($atts, $content = '') { static $year = null; static $month = null; static $session = null; static $round = null; $condition = $atts['condition'];; $value = $atts['value'];; switch ($condition) { case 'year': case 'month': case 'session': case 'round': if ($$condition != $value) { $$condition = $value; return $content; } break; } return ''; }
Can you please confirm it works as expected.
Looks perfect except for still needing the Game Number field as the final one. That was something I noticed I had to add *after* initially submitting this ticket. When I went to add the real results from the tournament, I saw that the client also had Game Number in the results spreadsheet he wanted me to emulate. I didn't notice that previously. So I added and populated that field on all the results from the 13th and that needs to be the last sort.
So the order will be
Date
Session
Round
Game Number
I've added the game number as well for the sorting and adjusted the code as given under at "Custom Code" section offered by Toolset:
add_filter('wpv_filter_query','func_sort_by_multiple_custom_fields', 199, 3); function func_sort_by_multiple_custom_fields($query_args, $view_settings, $view_id ) { $view_ids = array( 841 ); if( in_array( $view_id, $view_ids)) { $query_args['meta_query'] = array( 'relation' => 'AND', 'match_day_clause' => array( 'key' => 'wpcf-match-date-and-time', 'type'=>'NUMERIC', 'compare' => 'EXISTS', ), 'session_clause' => array( 'key' => 'wpcf-session2', 'compare' => 'EXISTS', ), 'round_clause' => array( 'key' => 'wpcf-round', 'compare' => 'EXISTS', ), 'game_num_clause' => array( 'key' => 'wpcf-game-number', 'type'=>'NUMERIC', 'compare' => 'EXISTS', ), ); $query_args['orderby'] = array( 'match_day_clause'=>'ASC', 'session_clause' => 'ASC', 'round_clause' => 'ASC', 'game_num_clause'=> 'ASC' ); } return $query_args; } add_shortcode('heading', 'my_heading'); function my_heading($atts, $content = '') { static $year = null; static $month = null; static $session = null; static $round = null; static $gamenumber = null; $condition = $atts['condition'];; $value = $atts['value'];; switch ($condition) { case 'year': case 'month': case 'session': case 'round': case 'gamenumber': if ($$condition != $value) { $$condition = $value; return $content; } break; } return ''; }
Can you please confirm it wroks as expected now: hidden link
Perfect!!! I only had to change one line to make the date order descending.
'match_day_clause'=>'DESC',
All good now.
hidden link
I'll be deleting all the non-May 13 matches after I show the client that the date grouping/sorting is working.
Thanks as always!
My issue is resolved now. Thank you!