• Resolved Sakshi Tyagi

    (@sakshityagi343)


    Hi,

    I’m trying to modify the search query using the asp_query_args filter, but it doesn’t seem to fire at all. I’ve added error_log('ASP Query Hook Triggered!');, but nothing appears in debug.log.

    Here’s my code:

    function wpsp_orderby_posts($vars) {  
    $params = wpsp_get_orderby_select();
    if ( !is_admin() && $vars->is_main_query() ) {
    $vars->set('meta_key', $params['meta_key']);
    $vars->set('orderby', $params['orderby']);
    $vars->set('order', $params['order']);
    $vars->set('ignore_sticky_posts', 1);
    return $vars;
    }
    }
    add_filter('pre_get_posts', 'wpsp_orderby_posts');

    Could you confirm if this hook is still working, or suggest an alternative way to sort results by a custom field?

    Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Ernest Marcinko

    (@ernestmarcinko)

    Hi!

    In the lite version the hook name is asl_query_args, however you don’t need that to order by custom field values, it is natively supported via back-end options under the General Options -> Ordering panel.

    All the best,
    Ernest

    • This reply was modified 2 weeks, 1 day ago by Ernest Marcinko. Reason: image embed
    Thread Starter Sakshi Tyagi

    (@sakshityagi343)

    Thankyou for your respone!

    Well, I’m aware of this ordering feature in the plugin settings. However, on the my website’s search result page I have a Sort dropdown thorough which I need to dynamically pass the meta key and meta value to the search query.

    Here is my search sorting dropdown https://prnt.sc/OcYBXdy76RAG

    I have updated my code to below:

    add_filter('asl_query_args', 'modify_asl_search_sorting', 10, 2);
    function modify_asl_search_sorting($args, $search_id) {
    $orderby_settings = wpsp_get_orderby_select();

    $args['meta_key'] = $orderby_settings['meta_key'];
    $args['orderby'] = $orderby_settings['orderby'];
    $args['order'] = $orderby_settings['order'];

    return $args;
    }

    wpsp_get_orderby_select() method gives me selected sorting data. For example this type of data:

    Array ( [1] => [2] => [3] => [4] => [5] => selected="selected" [6] => [meta_key] => year [orderby] => meta_value_num [order] => ASC )

    But the sorting is still not effective.

    Please help.

    Plugin Support Ernest Marcinko

    (@ernestmarcinko)

    Oh, I see. Some arguments are different in the search, this should do the trick:

    add_filter('asl_query_args', 'modify_asl_search_sorting', 10, 2);
    function modify_asl_search_sorting($args, $search_id) {
    $orderby_settings = wpsp_get_orderby_select();

    $args['post_primary_order'] = 'customfp ' . strtoupper($orderby_settings['order']) === 'ASC' ? 'ASC' : 'DESC';
    $args['post_primary_order_metatype'] = $orderby_settings['orderby'] === 'meta_value_num' ? 'numeric' : 'string';
    $args['_post_primary_order_metakey'] = $orderby_settings['meta_key'];

    return $args;
    }
    Thread Starter Sakshi Tyagi

    (@sakshityagi343)

    Thanks for your response!

    I tried your code but now I don’t see any results on my search results page after adding this code. I think its most probably due to this line of code


    $args['post_primary_order'] = 'customfp ' . strtoupper($orderby_settings['order']) === 'ASC' ? 'ASC' : 'DESC';

    When I commented above line, results showed, irrespective of the selected order filter. So, can you please confirm if its used correctly in the filter.

    Thread Starter Sakshi Tyagi

    (@sakshityagi343)

    I found the error in the pointed line of code. Below is the correction:

    $args['post_primary_order_metatype'] = 'customfp ' . strtoupper($orderby_settings['order']) === 'ASC' ? 'ASC' : 'DESC';

    //Here is the updated code
    add_filter('asl_query_args', 'modify_asl_search_sorting', 10, 2);
    function modify_asl_search_sorting($args, $search_id) {
    $orderby_settings = wpsp_get_orderby_select();
    $args['post_primary_order_metatype'] = 'customfp ' . strtoupper($orderby_settings['order']) === 'ASC' ? 'ASC' : 'DESC';
    $args['post_primary_order_metatype'] = $orderby_settings['orderby'] === 'meta_value_num' ? 'numeric' : 'string';
    $args['_post_primary_order_metakey'] = $orderby_settings['meta_key'];
    return $args;
    }

    But still order is not getting changed even though the selected filter is being passed in the search Query:

    Executed SQL: SELECT SQL_CALC_FOUND_ROWS  wp_posts.*
    FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
    WHERE 1=1 AND (((wp_posts.post_title LIKE '{0d77b9a9bf73277ef7db3f407eae97a0a2944832fb7814f9991298d8c351dc1e}WHITMAN{0d77b9a9bf73277ef7db3f407eae97a0a2944832fb7814f9991298d8c351dc1e}') OR (wp_posts.post_excerpt LIKE '{0d77b9a9bf73277ef7db3f407eae97a0a2944832fb7814f9991298d8c351dc1e}WHITMAN{0d77b9a9bf73277ef7db3f407eae97a0a2944832fb7814f9991298d8c351dc1e}') OR (wp_posts.post_content LIKE '{0d77b9a9bf73277ef7db3f407eae97a0a2944832fb7814f9991298d8c351dc1e}WHITMAN{0d77b9a9bf73277ef7db3f407eae97a0a2944832fb7814f9991298d8c351dc1e}'))) AND (
    wp_postmeta.meta_key = 'authorlast'
    ) AND ((wp_posts.post_type = 'aboutslider' AND (wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private')) OR (wp_posts.post_type = 'attachment' AND (wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private')) OR (wp_posts.post_type = 'book' AND (wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private')) OR (wp_posts.post_type = 'catalogues' AND (wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private')) OR (wp_posts.post_type = 'page' AND (wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private')) OR (wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private')) OR (wp_posts.post_type = 'slider' AND (wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private')))
    GROUP BY wp_posts.ID
    ORDER BY wp_postmeta.meta_value ASC
    LIMIT 0, 10

    ‘authorlast’ is the meta_key which I selected through sorting dropdown.

    Please guide!

    Plugin Support Ernest Marcinko

    (@ernestmarcinko)

    Hi again,

    The issue is a missing parenthesis, that messes up the post_primary_order, this is the correct code, I had tested this with manual values:

    add_filter('asl_query_args', 'modify_asl_search_sorting', 10, 2);
    function modify_asl_search_sorting($args, $search_id) {
    $orderby_settings = wpsp_get_orderby_select();

    $args['post_primary_order'] = 'customfp ' . ( strtoupper($orderby_settings['order']) === 'ASC' ? 'ASC' : 'DESC' );
    $args['post_primary_order_metatype'] = $orderby_settings['orderby'] === 'meta_value_num' ? 'numeric' : 'string';
    $args['_post_primary_order_metakey'] = $orderby_settings['meta_key'];
    return $args;
    }

    All the best,
    Ernest

    Thread Starter Sakshi Tyagi

    (@sakshityagi343)

    It’s working now! Thank you so much for all your time and assistance. I truly appreciate it! ??

    Plugin Support Ernest Marcinko

    (@ernestmarcinko)

    That’s great!

    I will mark this topic as resolved then!

    Feel free to rate the plugin if you like it, it’s greatly appreciated.

    All the best,
    Ernest

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.