relevanssi_excerpt_content

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

Filters the post content in the excerpt-building process after the_content filter has been applied 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

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

When Relevanssi builds an excerpt for a post in relevanssi_do_excerpt(), the post content is first fetched from $post->post_content. The custom field content is added 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, 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