Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › re-use query sort order as search order with Ajax Search Pro.
This topic contains 7 replies, has 2 voices, and was last updated by Ernest Marcinko 1 year, 9 months ago.
- AuthorPosts
- June 9, 2021 at 4:53 pm #33578
A question:
In a search instance, you define the search order. Is there any way I can skip that and re-use the wp_query settings on an archive page?I use the filtering and live search in combination with the elementor posts or archive posts widgets on different pages, and I edit the query with pre_get_posts for different archive pages and controlled in the URL. I would like to re-use those queries even with the ajax live search, maintaining the search order and not change the search order with Ajax Search Pro.
Pls find this page as an example:
https://sensative.com/iot-use-cases-by-sensative/?orderby=views&order=descIs there a simple solution? I didn’t find something like a “default query” or something in the dropbox in the settings. Or can I use an asp_query_args filter?
How would I go about solving this? Is it possible? Any suggestions would be much appreciated π
June 9, 2021 at 8:23 pm #33589Hi,
Well, on the search results page some of these arguments are respected, but with the elementor live filter it is handled differently. Indeed using the asp_query_args filter is in my opinion the best option here. You can use these arguments to change the ordering.
Best,
However please note, that in the sample URL you had order by “views”, which is probably some sort of a custom field, so changing any of those argumens to something like “views DESC” will not do anything – as that field does not exist in the query.
You may also need to inject a sub select or something to the select statement to have the desired fields via these filters. Please be very careful when making direct changes to the final query.
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
June 10, 2021 at 9:31 am #33595Thanks,
what I more or less expected πAnyway, a poor example. I modify the query sort order with a meta-query sort on my custom field views. And I take that from the URL, to make it identical to the URLs for date and alphabetical sort order.
https://sensative.com/use_case_market/smart-building/?orderby=date&order=desc
https://sensative.com/use_case_market/smart-building/?orderby=title&order=asc
https://sensative.com/use_case_market/smart-building/?orderby=views&order=descWordpress is kind enough to manage the re-sort based on the URL and my hack implements the views to function identically as the standard WordPress.
So, if I can make ASP respect the WordPress query on these search results and archive pages I am all done π
Thanks, I’ll continue hacking based on your directions πBR
FredrikJune 11, 2021 at 11:36 am #33615I am working on this now. One piece of information that is missing from the documentation:
https://knowledgebase.ajaxsearchpro.com/hooks/filters/query-and-output/asp_query_args#primary-and-secondary-orderingYou can set the primary search order to
Primary ordering: Custom Field descending
custom field name: views
type: numerichow would you use that in
$args[‘post_primary_order’] = “relevance DESC”;?
June 11, 2021 at 1:38 pm #33617Found it by reading your code:
$args[‘post_primary_order_metatype’] = ‘numeric’;
π
/Fredrik
June 11, 2021 at 3:32 pm #33620Great π
The relevant fields in this case are these:
'post_primary_order' => "relevance DESC", // CAN be a custom field name 'post_secondary_order' => "post_date DESC", 'post_primary_order_metatype' => false, // false (if not meta), 'numeric', 'string' 'post_secondary_order_metatype' => false, // false (if not meta), 'numeric', 'string' '_post_primary_order_metakey' => false, // gets parsed later, do not touch '_post_secondary_order_metakey' => false, // gets parsed later do not touch
The _post_primary_order_metakey and the _post_secondary_order_metakey are the meta field name, the field you mentioned is the meta type.
Also, when using the meta field ordering a sub query is created to join the meta field values to the main query in the main select. The select field name is “customfp” and “customfs”.So when you set the
post_primary_order_metatype
and the_post_primary_order_metakey
, then also use:
$args['post_primary_order'] = "customfp DESC";
..for the secondary:
Best,$args['post_primary_order'] = "customfs DESC";
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
June 11, 2021 at 3:49 pm #33621Gotcha!
Thanks, it works perfectly!
BR
FredrikJune 14, 2021 at 7:26 am #33634You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.