Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Implement a sorting dropdown
- This topic has 5 replies, 2 voices, and was last updated 8 months, 1 week ago by
Ernest Marcinko.
-
AuthorPosts
-
September 25, 2025 at 9:25 am #55580
devopstools_sPAZ
ParticipantHello
We need to implement sort by date ascending/descending besides alphabetical sort.
It seems the sort functionality is not shipped with the plugin by default.
Can you please confirm. and if not is there a custom way to implement it (if so please attach related documentation)
Best Regards
September 25, 2025 at 9:50 am #55583Ernest Marcinko
KeymasterHi,
I’m afraid this is not possible, there is no sorting filter available as of now for the live results.
I usually recommend a customization or a custom code where possible, but this is a lot more complicated than that. The only way to achieve this is to fully implement it from top to bottom, which unfortunately takes more than a few lines of code.
It will be available though in a later release, as it is a planned feature within the filters section rework.
September 25, 2025 at 1:26 pm #55586devopstools_sPAZ
Participantthanks for your response.
if we created the date sort dropdown and made it adding the query parameters to the URL.
is there a way to hack into the search query to add the sorting parameters.
Best
September 26, 2025 at 8:01 am #55592Ernest Marcinko
KeymasterOn the search results page it is actually possible without any additional code, as it will recognize the
$_GET['order']and$_GET['orderby']parameters.All you need to do is append that to the end of URL as query parameters, like:
...&order=ASC&orderby=idThe possible supported values by default are:
order parameter:ASC, DESC
orderby parameter:id, post_id, product_id, popularity, post_popularity, product_popularity, rating, post_rating, product_rating, date, post_date, product_date, name, post_name, alphabetical, price, relevanceI suggest using these if you can. If you want you can custom code additional accepted values as you like, let me know and I will let you know a custom code snippet.
September 29, 2025 at 6:40 am #55602devopstools_sPAZ
ParticipantYes if you can provide me with a code snippet will help a lot.
Much appreciated.September 29, 2025 at 9:50 am #55605Ernest Marcinko
KeymasterOkay.
So say you have a single drop-down, which appends the “the_order” query variable, so the URL looks like this “…&the_order=yourvalue” on the results page. Then it is accessed on page load within the code via the
$_GET['the_order'].Here is a custom code snippet which handles that, it has examples for id, date, title, relevance and even string and numeric custom field based ordering:
// Custom ordering add_filter( 'asp_query_args', function ( $args ) { // The values allowed in the the_ordering variable $allowed_orderings = array( 'relevance', 'id', 'id_reverse', 'date', 'date_reverse', 'alpha', 'alpha_reverse', 'numeric_custom_field', 'string_custom_field', ); if ( !isset($_GET['the_order']) ) { return $args; } $ordering = sanitize_text_field( wp_unslash( $_GET['the_order'] ) ); if ( !in_array($ordering, $allowed_orderings, true) ) { return $args; } switch ( $ordering ) { case 'id': $args['post_primary_order'] = 'id DESC'; break; case 'id_reverse': $args['post_primary_order'] = 'id ASC'; break; case 'date': $args['post_primary_order'] = 'post_date DESC'; break; case 'date_reverse': $args['post_primary_order'] = 'post_date ASC'; break; case 'alpha': $args['post_primary_order'] = 'post_title DESC'; break; case 'alpha_reverse': $args['post_primary_order'] = 'post_title ASC'; break; case 'numeric_custom_field': $args['post_primary_order'] = 'customfp DESC'; $args['post_primary_order_metatype'] = 'numeric'; $args['_post_primary_order_metakey'] = 'custom_field_name'; break; case 'string_custom_field': $args['post_primary_order'] = 'customfp DESC'; $args['post_primary_order_metatype'] = 'string'; $args['_post_primary_order_metakey'] = 'custom_field_name'; break; case 'relevance': default: $args['post_primary_order'] = 'relevance DESC'; break; } return $args; } ); -
AuthorPosts
- You must be logged in to reply to this topic.