relevanssi_custom_field_value

apply_filters( 'relevanssi_custom_field_value', array $meta_value, string $meta_key, int $post_id)

Filters the custom field values before Relevanssi indexes them or uses them in creating excerpts.

Parameters

$meta_value
(array) An array of custom field values from get_post_meta().

$meta_key
(string) The name of the custom field.

$post_id
(int) The post ID.

More information

This filter function can be used to modify the contents of custom fields before Relevanssi uses them.

Note that Relevanssi always calls get_post_meta() with the $single parameter set to false, so this hook always get an array, even if there’s just one custom field value, and any filter functions are always expected to return an array.

If you want to blank out a custom field, it’s best to replace the $meta_value with an empty array to avoid problems with other functions on this hook. When writing your own functions, it may be wise to expect the incoming value can also be an empty value.

If you have shortcodes in custom fields you want to expand in the indexing, you can do that with this filter:

add_filter( 'relevanssi_custom_field_value', 'rlv_expand_shortcodes_in_fields' );
function rlv_expand_shortcodes_in_fields( $values ) {
  $new_values = array();
  foreach ( $values as $value ) {
    $new_values[] = do_shortcode( $value );
  }
  return $new_values;
}

Default filters

By default Relevanssi adds a filter function relevanssi_filter_custom_fields() to this hook on priority 10. This function filters out unwanted custom fields Relevanssi doesn’t want to index. At the moment it blanks out the custom fields classic-editor-remember and php_everywhere_code, both of which contain content Relevanssi users should not be interested in.

function relevanssi_filter_custom_fields( $values, $field ) {
	$unwanted_custom_fields = array(
		'classic-editor-remember' => true,
		'php_everywhere_code'     => true,
	);
	if ( isset( $unwanted_custom_fields[ $field ] ) ) {
		$values = array();
	}
	return $values;
}

Relevanssi Oxygen compatibility uses relevanssi_custom_field_value to manipulate and clean up the contents of the ct_builder_shortcodes custom field before Relevanssi indexing.