relevanssi_excerpt_content

apply_filters( 'relevanssi_excerpt_content', string $content, object $_post, string $query )

Filters the post content in the excerpt-building process after Relevanssi applies the the_content filter to the content.

Parameters

$content
(string) The post content.

$_post
(object) The post object. Usually a WP_Post object, but not always.

$query
(string) The search query.

More information

relevanssi_excerpt_content is one of the superstar filter hooks in Relevanssi, one of the more convenient and valuable. If you want to add extra content to the excerpts or prevent some post content from appearing in the excerpts, this is generally the best place.

When Relevanssi builds an excerpt for a post in relevanssi_do_excerpt(), Relevanssi first fetches the post content from $post->post_content. Then Relevanssi adds custom field content to the end of the content according to the settings. Then the content is passed through the relevanssi_pre_excerpt_content filter hook, the_content filters are applied, and then the content passes through this filter hook.

After this filter hook is applied, Relevanssi strips all tags and removes linefeeds and carriage returns, and then the excerpts are created with relevanssi_create_excerpts().

For example, if you don’t mind indexing pull quotes but don’t want them used for excerpts, this function removes pull quotes from posts before Relevanssi generates the excerpts:

add_filter( 'relevanssi_excerpt_content', 'rlv_remove_pull_quotes' );
function rlv_remove_pull_quotes( $content ) {
    return preg_replace( '#<figure class="wp-block-pullquote".*?></figure>#', '', $content );
}

To remove all parts of the post marked with the relevanssi_noindex class from the excerpts, you can use this:

add_filter( 'relevanssi_excerpt_content', 'rlv_remove_noindex_class' );
function rlv_remove_noindex_class( $content ) {
    return preg_replace( '#<(.*) class=".*?relevanssi_noindex".*?</\1>#ms', '', $content );
}

Related knowledge base entries