relevanssi_excerpts

apply_filters( 'relevanssi_excerpts', array $excerpts, int $post_id )

In Relevanssi Premium, filters all the individual excerpt parts found before the highlighting is applied and the excerpts used are chosen.

Parameters

$excerpts
(array) An array of excerpt parts. text has the excerpt text, start is a Boolean flag that is true if the excerpt is from the beginning of the post, source is the part of the post where the excerpt is from and hits is the number of search term matches in the part.

$post_id
(int) The post ID.

More information

In Relevanssi Premium, the excerpt parts – whether multi-part excerpts are used or not – are passed through this filter. This happens in the relevanssi_combine_excerpts() function, which takes all the excerpts from different sources (post content, comments, excerpt), combines them together, sorts them by the number of hits (the ones with most search terms hits first), passes the array of excerpts through this filter hook and then takes the n best excerpts where n is the number of excerpts requested.

This filter hook can be used to modify the set of excerpts before the best ones are chosen. If you want to eliminate some excerpts, this filter hook can be used. For example, to eliminate comment excerpts from the post 123 but use them elsewhere, you can do this:

add_filter( 'relevanssi_excerpts', 'rlv_modify_excerpts', 10, 2 );
function rlv_modify_excerpts( $excerpts, $post_id ) {
  if ( 123 === $post_id ) {
    $excerpts = array_filter(
      $excerpts,
      function( $excerpt ) {
        return $excerpt['source'] !== 'comments';
      }
    );
  }
  return $excerpts;
}