apply_filters( 'relevanssi_index_custom_fields', array $custom_fields, int $post_id )

Filters the list of custom field names to index before the indexing.


(array) An array of custom field names.

(int) The post ID.

More information

When Relevanssi indexes the custom fields of a post or uses custom fields to generate an excerpt, a list of custom fields is fetched with the relevanssi_generate_list_of_custom_fields() function. It checks the relevanssi_index_fields option, which either specifies all, visible or a list of custom field names. A list is a list, while all means all custom fields for the post and visible means all custom fields with names that don’t begin with an underscore, ie. all the custom fields that appear on the post edit page.

The all and visible settings are then converted into lists of actual custom field names. Relevanssi Premium also converts the ACF repeater field name patterns (field_%_subfield) into actual field names (field_1_subfield, field_2_subfield etc) at this point. Then the list of field names is passed through this filter and then relevanssi_generate_list_of_custom_fields() returns it.

This filter hook can thus be used to add or remove custom fields from the list of fields to index. This is particularly helpful especially in those cases where you want to index most of the fields or most of the visible fields, but want to exclude couple of fields. It’s much easier to just have Relevanssi index all or visible and then exclude the specific fields, than to list all the fields you want to include.

Here’s how that would work in the theme functions.php:

add_filter( 'relevanssi_index_custom_fields', 'rlv_skip_custom_fields' );
function rlv_skip_custom_fields( $custom_fields ) {
  $unwanted_fields = array( 'not_this_field', 'not_this_one_either' );
  $custom_fields   = array_diff( $custom_fields, $unwanted_fields );
  return $custom_fields;

Another possible use case is a complicated ACF setup where you have lots of flexible elements. Let’s assume we have many layers and levels and lots of elements, but we only want to index the text elements. These are all named so that the field name ends with _text, no matter where it is in the hierarchy. These are the only custom fields we want to index. So, let’s set Relevanssi to index all fields, and then use this to choose what we actually index:

add_filter( 'relevanssi_index_custom_fields', 'rlv_only_text_fields' );
function rlv_only_text_fields( $custom_fields ) {
  return array_filter(
    function( $field ) {
      return '_text' === substr( $field, -5 );

This would only index fields that end with _text, without us needing to figure out where exactly in the field hierarchy the text elements appear.