Forums › Forums › Search & Filter Pro › How to change search results sort order
Tagged: alphabetical, sort order
- This topic has 27 replies, 3 voices, and was last updated 3 years, 10 months ago by Ross.
-
Anonymous(Private) December 18, 2020 at 3:15 pm #270290
Hi, thanks for this great plugin. We’re trying to sort search results alphabetically using a custom field in the post. My programmer wrote some code that sorts them that way on the archive pages, but it’s not working for the search&filter results page. How can we achieve this? Thanks for your help!
Trevor(Private) December 18, 2020 at 5:07 pm #270325If you want to use custom code (PHP), you will need to use this filter (it accepts arguments in the same way as wp_query):
https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments
Anonymous(Private) December 18, 2020 at 9:59 pm #270369Thanks, but my programmer can’t quite figure out what you meant here. Here’s the code we used to sort the results and it works on other pages but not with your search results.
add_action( ‘pre_get_posts’, ‘wpse183601_filter_category_query’ );
function wpse183601_filter_category_query( $query ) {
if( is_tax() && !is_admin() && $query->is_main_query() ) {
$query->set( ‘orderby’,’meta_value’ );
$query->set( ‘meta_key’,’last_name’ );
$query->set( ‘order’,’ASC’ );
}
}Can you please let me know how to get the above to work on your results pages? Thank you so much for your help.
Trevor(Private) December 21, 2020 at 3:20 pm #270546It might be an issue with the PHP variable max_input_vars (the PHP installation default for that variable is 1000, which is rarely anywhere near enough for WordPress).
So, that would be something I would check. The setting can be seen in WordPress admin, here:
-
AuthorPosts