relevanssi_excerpt

apply_filters( 'relevanssi_excerpt', string $excerpt, int $post_id )

Filters the Relevanssi excerpt before highlighting is applied and the ellipsis is added. If you are using multi-part excerpts, this filter fires separately for each individual excerpt part.

Parameters

$excerpt
(string) The excerpt text.

$post_id
(int) The post ID.

More information

When Relevanssi creates excerpts, the process happens in relevanssi_do_excerpt(). Terms are processed, shortcodes are managed, the_content filters are applied, tags are stripped and finally the excerpt is created.

The excerpt is then passed through this filter hook. After that the search term highlighting is added, an ellipsis is added and finally the excerpt is returned.

This filter hook is often not that useful. If you want to modify what goes in the excerpt, you want relevanssi_excerpt_content, and if you want to modify the ready excerpt element, you want relevanssi_excerpt_part. I had some difficulties coming up with a meaningful example here. This example replaces the excerpt on password protected posts with a “buy access” link:

add_filter( 'relevanssi_excerpt', 'rlv_modify_excerpt', 10, 2 );
function rlv_modify_excerpt( $excerpt, $post_id ) {
  $post = relevanssi_get_post( $post_id );
  if ( $post->post_password ) {
    $excerpt = '<a href="/buy-access">Buy access to protected posts here</a>';
  }
  return $excerpt;
}