Forums › Forums › Search & Filter Pro › A third sort order?
Tagged: orderby, results, sort order
- This topic has 4 replies, 2 voices, and was last updated 4 years, 7 months ago by Trevor.
-
Anonymous(Private) May 5, 2020 at 6:05 pm #242726
Is it possible to control the sort order beyond the two sort order options available in the Posts tab in the searchandfilter settings?
We can’t use relevanssi because we have 12,000 posts.
I have set the search to sort by one meta value first (subject_code), and then another (course_number). After that, I’d like it to sort by a third meta value (start_date). Can I programmatically add a third somehow using an action hook?
Or, at the very least could I have it sort alphabetically after the first two?
Thank you!
Trevor(Private) May 6, 2020 at 7:23 am #242759You would need to return the sort order settings in the Posts tab back to default, and instead see if you can use this filter:
https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments
This filter uses the same argument structure as wp_query.
Anonymous(Private) May 6, 2020 at 4:07 pm #242855Thank you. I restored the Posts settings to the default order and used the following code, which is not affecting the sort order:
function filter_function_name( $query_args, $sfid ) { if($sfid==23142) { $query_args["orderby"] = array( 'subject_code' => 'asc', 'course_number' => 'asc', 'start_date' => 'asc', ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
It works if I set just a single order like so:
function filter_function_name( $query_args, $sfid ) { if($sfid==23142) { $query_args["orderby"] = 'subject_code'; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Do I need to format the $query_args differently? That’s how I do it in a regular WP_Query…
Thank you!
Anonymous(Private) May 6, 2020 at 4:35 pm #242872Nevermind. I didn’t realize I had to also manually add the meta_query to get the search order to work. Here is my working code:
function filter_function_name( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==23142) { $query_args["meta_query"] = array( 'relation' => 'AND', 'subject_code' => array( 'key' => 'subject_code', 'compare' => 'EXISTS', ), 'course_number' => array( 'key' => 'course_number', 'compare' => 'EXISTS', ), 'start_date' => array( 'key' => 'start_date', 'compare' => 'EXISTS', ), ); $query_args["orderby"] = array( 'subject_code' => 'asc', 'course_number' => 'asc', 'start_date' => 'asc', ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Please feel free to close this ticket. Thank you!
-
AuthorPosts