How to search using rendered content (shortcodes and reusable blocks)

By default, ElasticPress will index and use in its searches the post_content value as it is stored in the database. In some cases, it is preferable to use the final version of post_content, with its shortcodes, reusable blocks, and all the transformations made by WordPress with the the_content filter.

ElasticPress also indexes this transformed version of post_content and it can be used in searches. It is important to notice that this transformed version is generated at the moment the content is indexed, though. That means if you have a shortcode that displays the current time of the day, for example, ElasticPress will store in Elasticsearch a version of that content with the exact time when the content was saved and not the time when the user is searching for it.

If you want to use that field in addition to the regular post_content value, you can add the following snippet to your theme’s functions.php or into a standalone plugin:

PHP
add_filter(
    'ep_weighting_configuration_for_search',
	function( $weight_config ) {
		foreach ( $weight_config as &$post_type_config ) {
			if ( isset( $post_type_config['post_content'] ) ) {
				$post_type_config['post_content_filtered'] = $post_type_config['post_content'];
			}
		}
		return $weight_config;
	}
);

Since the introduction of Instant Results in ElasticPress 4.0, another field was made available if that feature is enabled: post_content_plain, which has the same content as post_content_filtered but without any HTML tags. The removal is done with WordPress’s wp_strip_all_tags() function, meaning that content of links and paragraphs, for example, are preserved but content of <script> and <style> tags are completely removed. See the table below to understand better which one of those fields is used:

NameDescriptionIndexed as
post_contentThe raw content of the post, as in the database.<!-- wp:paragraph --><p>Post content </p>
<!-- /wp:paragraph -->
post_content_filteredThe post content after being filtered by the the_content filter.<p>Post content</p>
<script>
console.log("hi");</script>
post_content_plainSame as post_content_filtered but after wp_strip_all_tags() call.Post content