apply_filters( 'relevanssi_multi_results', array $results )
Filters the results as post ID => weight pairs from the multisite search. Serves the same purpose as relevanssi_results
does for single-site searches.
Parameters
$results
(array) An array where the keys are blog ID|post ID and values are the post weights.
More information
The relevanssi_results
filter hook is a fine tool for adjusting the search results based on the post ID, but it has not been available in multisite searching. This filter hook introduces the same filtering to multisite searching. The data this hook filters is the same, but relevanssi_results
filter functions don’t work directly with this hook.
This filter hook filters an array of post ID and weight pairs. The array this filter returns is then sorted by the weight in descending order, so you shouldn’t worry about the order of this array. Any modifications to the order of results must be done by changing the weights.
Because this hook as results from multiple blogs, instead of just post ID the keys will also have the blog ID. The format is blog ID|post ID
, eg. 1|42
would be the post ID 42 from blog 1.
For example, let’s take this relevanssi_results
filter:
add_filter( 'relevanssi_results', 'date_weights' ); function date_weights( $results ) { array_walk( $results, function( &$weight, $post_id ) { $post_date = get_the_time( 'U', $post_id ); if ( time() - $post_date < WEEK_IN_SECONDS ) { $weight = $weight * 2; } else { $weight = $weight / 2; } } ); return $results; }
It would convert to relevanssi_multi_results
like this:
add_filter( 'relevanssi_multi_results', 'date_weights' ); function date_weights( $results ) { array_walk( $results, function( &$weight, $blog_post_id ) { list ( $blog_id, $post_id ) = explode( '|', $blog_post_id ); switch_to_blog( $blog_id ); $post_date = get_the_time( 'U', $post_id ); if ( time() - $post_date < WEEK_IN_SECONDS ) { $weight = $weight * 2; } else { $weight = $weight / 2; } restore_current_blog(); } ); return $results; }
Another alternative for this filter hook is relevanssi_site_results
, which is compatible with relevanssi_results
filter hooks, because it filters individual site results in the multisite search (so this filter hook will run multiple times in the multisite search, once for each searched subsite).
The above function would probably be better done using the relevanssi_results
filter function on relevanssi_site_results
, because that way we could avoid the multiple switch_to_blog()
operations. relevanssi_multi_results
is best left for cases where you need to see the whole set of results for some reason.