apply_filters( 'relevanssi_content_to_index', string $content, WP_Post $post_object ) )
Adds new content to posts before indexing.
Parameters
$content
(string) The added content.
$post_object
(WP_Post) The post object.
More information
The $content
starts empty; this filter hook does not filter the actual post content (relevanssi_post_content
is for that). This hook just lets you add extra content to the post. Still, it’s best to assume the $content
has something and to append to it, starting with a space, in order to avoid problems if another filter function has already added something.
Here’s a basic example of fetching data from a database table to add to the content:
add_filter( 'relevanssi_content_to_index', 'rlv_add_extra_content', 10, 2 ); function rlv_add_extra_content( $content, $post ) { global $wpdb; $content .= ' ' . $wpdb->get_var( "SELECT data FROM extra_table WHERE post_id = $post->ID" ); return $content; }
Do note that whenever the content changes, Relevanssi index is not updated. Relevanssi index is only updated when the post is saved or the whole index is rebuilt.
If you want the additional content to appear in excerpts as well, you can use the same function on the relevanssi_excerpt_content
filter hook, but in this case you must retain the original contents of $content
:
add_filter( 'relevanssi_content_to_index', 'rlv_add_extra_content', 10, 2 ); add_filter( 'relevanssi_excerpt_content', 'rlv_add_extra_content', 10, 2 ); function rlv_add_extra_content( $content, $post ) { global $wpdb; $content .= ' ' . $wpdb->get_var( "SELECT data FROM extra_table WHERE post_id = $post->ID" ); return $content; }
This adds the content both to index and to excerpts.
A note on performance
If you’re doing something potentially slow or heavy, for example fetching content from an external website, it’s best to use separate functions for indexing and excerpts. The indexing function is only run when the post is saved, but the excerpt function is run on every search where the post appears. Thus you don’t want to do heavy lifting in the excerpt function.
For example if you want to fetch the contents from an external URL as part of the post content, you could do it like this:
add_filter( 'relevanssi_content_to_index', 'rlv_add_external_content', 10, 2 ); function rlv_add_external_content( $content, $post ) { $url = get_post_meta( $post->ID, 'external_content_url', true ); if ( filter_var( $url, FILTER_VALIDATE_URL ) !== false ) { $response = wp_remote_get( $url ); if ( is_wp_error( $response ) ) { return $content; } $body = wp_remote_retrieve_body( $response ); $content .= ' ' . $body; update_post_meta( $post->ID, '_external_content_body', $body ); return $content; } add_filter( 'relevanssi_excerpt_content', 'rlv_excerpt_external_content', 10, 2 ); function rlv_excerpt_external_content( $content, $post ) { $content .= ' ' . get_post_meta( $post->ID, '_external_content_body', true ); return $content; }
When the content is fetched during the indexing, it is saved in the post custom fields, and the excerpt function takes it from there, without a slow remote request.