• Resolved sybrenwebmixnl

    (@sybrenwebmixnl)


    We’re using Relevanssi with Search & Filter Pro on our website. On the main search page, we’d like to sort the results by post type, prioritizing the products over posts and pages. I tried the following functions:

    add_filter( 'relevanssi_comparison_order', 'rlv_post_type_order' );
    function rlv_post_type_order( $order_array ) {
        $order_array = array(
            'product' => 0,
            'page' => 1,
            'post' => 2,
        );
        return $order_array;
    }
    
    add_filter( 'relevanssi_modify_wp_query', 'rlv_orderby' );
    function rlv_orderby( $query ) {
        $query->set( 'orderby', 'post_type' );
        return $query;
    }

    and

    add_filter( 'relevanssi_hits_filter', 'products_first' );
    function products_first( $hits ) {
        $types = array();
    
        $types['page']    = array();
        $types['post']    = array();
        $types['product'] = array();
     
        // Split the post types in array $types
        if ( ! empty( $hits ) ) {
            foreach ( $hits[0] as $hit ) {
                array_push( $types[ $hit->post_type ], $hit );
            }
        }
    
        // Merge back to $hits in the desired order
        $hits[0] = array_merge( $types['product'], $types['post'], $types['page'] );
        return $hits;
    }

    However, neither of them seem to work. The relevanssi_hits_filter returns an empty results page, even after rebuilding the index. The relevanssi_comparison_order just returns the results, ignoring the defined order.

    Any ideas? Help would be much appreciated!

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Try removing the Search & Filter from the equation. What if you use the Relevanssi admin search (Dashboard > Admin search)? Does that work with the relevanssi_comparison_order function?

    It’s likely relevanssi_modify_wp_query does not trigger at all when using Search & Filter (I’m not sure how it’s implemented, but if it’s calling relevanssi_do_query() directly like it should, relevanssi_modify_wp_query does not fire at all).

    I think going with the relevanssi_comparison_order is the way to go, but the key is to get the orderby parameter in from another place. Perhaps it’s pre_get_posts, perhaps some filter hook in Search & Filter – that’s something you probably should ask from the Search & Filter support, they’ll know better.

    Thread Starter sybrenwebmixnl

    (@sybrenwebmixnl)

    Hi Mikko,

    Thank you for your reply!

    Eventually I managed to get the relevanssi_hits_filter working when I disabled Search & Filter Pro. So it looks like you’re right regarding the relevanssi_do_query() function.

    I’ll contact Search & Filter Pro to see if they can help me.

    Thank you once again!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ordering results by post type not working (with SF pro)’ is closed to new replies.