apply_filters( 'relevanssi_results', array $post_weights )

This filter hook filters the results Relevanssi finds.


(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 searching process.

After this filter hook, Relevanssi sorts the array in descending order by the 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 for relevanssi_results is those cases where 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.

More examples:

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.