re-use query sort order as search order with Ajax Search Pro.

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 Ernest Marcinko 1 year, 3 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #33578
    sensafred31
    sensafred31
    Participant

    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=desc

    Is 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 πŸ™‚

    #33589
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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.
    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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #33595
    sensafred31
    sensafred31
    Participant

    Thanks,
    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=desc

    Wordpress 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
    Fredrik

    #33615
    sensafred31
    sensafred31
    Participant

    I 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-ordering

    You can set the primary search order to
    Primary ordering: Custom Field descending
    custom field name: views
    type: numeric

    how would you use that in
    $args[‘post_primary_order’] = “relevance DESC”;

    ?

    #33617
    sensafred31
    sensafred31
    Participant

    Found it by reading your code:

    $args[‘post_primary_order_metatype’] = ‘numeric’;

    πŸ™‚

    /Fredrik

    #33620
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Great πŸ™‚

    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:

    $args['post_primary_order'] = "customfs DESC";

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #33621
    sensafred31
    sensafred31
    Participant

    Gotcha!

    Thanks, it works perfectly!

    BR
    Fredrik

    #33634
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.