This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Search result page problem

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Search result page problem

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #51914
    dermonotonedermonotone
    Participant

    Hi there,
    i have problems with my search result page.
    In Ajax search pro the sroting of products is okay it does sort the products _price ASC.

    But on the search result page the products are not sorted by _price. it is random.
    Any help how i can archive the sorting on search page too? (should also be _price ASC).
    i treid this code but then it does not respect the search terms and i get more products then searched (eg when i search blue shirt it shows all prodiucts with blue OR shirt. it should be blue AND shirt)

    function custom_woocommerce_get_catalog_ordering_args( $args ) {
        if ( is_search() ) {
            $args['orderby'] = 'meta_value_num';
            $args['order'] = 'ASC';
            $args['meta_key'] = '_price';
        }
        return $args;
    }
    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
    
    function custom_search_order_by_price( $clauses, $query ) {
        global $wpdb;
        if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
            if ( strpos( $clauses['join'], 'pm.meta_key = \'_price\'' ) === false ) {
                $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS pm ON {$wpdb->posts}.ID = pm.post_id ";
            }
            $clauses['where'] .= " AND pm.meta_key = '_price' ";
            $clauses['orderby'] = " CAST(pm.meta_value AS DECIMAL) ASC ";
        }
        return $clauses;
    }
    add_filter( 'posts_clauses', 'custom_search_order_by_price', 10, 2 );

    thanks for oyur help

    #51921
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    The sorting should be the same if the search override is active.
    I tried a few queries but the ordering seems to be all right on my end: https://i.imgur.com/esKZtzl.png
    Everything seems to be in the same order. Did you manage to resolve it, or do you get different results?

    #51922
    dermonotonedermonotone
    Participant

    Hi,
    yes i was able to fix it. in search override i had to switch from post to get…

    sorry and thanks gain!

    #51923
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

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