relevanssi_results

apply_filters( 'relevanssi_results', array $post_weights )

This filter hook filters the results Relevanssi finds.

Parameters

$post_weights
(array) An array of (post ID, weight) pairs.

More information

When Relevanssi performs a search, there’s a database query for each search term. Relevanssi collects all the posts it finds with these queries to an array of (post ID, weight) pairs. Relevanssi then applies this filter to that array before continuing the search process.

After this filter hook, Relevanssi sorts the array in descending order by weight. Relevanssi then filters out all the posts that don’t match every search term if you run an AND search. Then Relevanssi converts the post IDs to the desired format (usually post objects) and returns an array of results.

Relevanssi has a trio of filter hooks that you can use to adjust the results:

  • relevanssi_match filters individual search term hits.
  • relevanssi_results filters the array of (post ID, weight) pairs.
  • relevanssi_hits_filter filters the final result set.

If you want to modify the results, one of these filter hooks is likely the best match. The best use case for the relevanssi_results hook is when you want to alter the weights of the posts based on the post ID. The relevanssi_match hook offers more information but is worse for performance because it can run hundreds of times per search. The relevanssi_hits_filter again provides more information but happens later in the process, and sometimes, it’s better to filter out posts earlier.

There are lots of examples of using this filter hook and relevanssi_match here.

Note: zero weight doesn’t work here

If you want to remove a post from the results, you can’t set its weight to zero. That works with relevanssi_match, but not here: the post will still be included in the results, just with zero weight. If you want to remove items from the search results using relevanssi_results, you need to remove them from the array completely.

Examples

More examples:

Increasing the results threshold

By default, the threshold for search results is zero: any post with a positive score gets included in the search results. If you want to increase that threshold, this filter hook is the best tool. Add this to your site and adjust the weight threshold from 100 to whatever works for you:

add_filter( 'relevanssi_results', function( $results ) {
    return array_filter( $results, function( $weight ) {
        return $weight > 100;
    } );
} );

The Relevanssi admin search is a good way to determine a suitable threshold, as it shows each post’s relevance scores.

Boosting individual posts

This snippet can boost individual posts (for example, to make certain posts rank higher among pinned posts). Enter the IDs of the posts in the $boosted_posts array.

add_filter( 'relevanssi_results', function( $postid_weight) {
    array_walk( $postid_weight, function( &$weight, $post_id ) {
        $boosted_posts = array( 22271 );
        if ( in_array( $post_id, $boosted_posts, true ) ) {
            $weight *= 100;
        }
    });
    return $postid_weight;
} );

Multisite searching

This filter hook is not available in multisite searching. The relevanssi_site_results hook is the equivalent filter in multisite searching, and there’s also relevanssi_multi_results for filtering the whole result set.