Indexing Shortcodes within custom field contents

How to index shortcode contents, which are within custom field contents.

By default shortcodes are not executed in custom field contents before indexing to avoid some known issues. In rare cases - such as using an ACF field with a Tablepress or any other shortcode it is neccessary.

Steps to get a shortcode indexed in a custom field

  • Configure the index table engine as you need it, but don't index yet (Don't click Create New index button, if you did, just reload the page)

  • Make sure that the custom field, which contains the shortcode(s) is also selected. In this example I'm using "field_name" as the field.

  • Use the custom code below. Where do I put the custom code?

  • Make sure to change the $field_name variable to the custom field, which contains the shortcode(s)

  • Now create the index

  • Make sure that the index table engine is enabled on the search instance settings as well

The custom code snippet to index custom field with shortcodes:

add_filter(
	'asp_post_custom_field_before_tokenize',
	function ( $values, $the_post, $field ) {
		$field_name = 'field_name';

		// Do not change anything below
		$new_values = array();
		if ( $field === $field_name ) {
			foreach( $values as $value ) {
				$new_values[] = do_shortcode($value);
			}
		}

		return $new_values;
	},
	10,
	3
);

Last updated