apply_filters( 'relevanssi_index_custom_fields', array $custom_fields, int $post_id )
Filters the list of custom field names to index before the indexing.
Parameters
$custom_fields
(array) An array of custom field names.
$post_id
(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 (if you want to add patterns from code, you can use the relevanssi_custom_fields_before_repeaters
filter hook, which runs before this filter hook). Then the list of field names is passed through this filter and then relevanssi_generate_list_of_custom_fields()
returns it.
Remove unwanted fields
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 a 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 (add this function to your site):
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; }
Include fields based on partial name
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( $custom_fields, 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.
Exclude fields based on partial name
If you want the opposite, where you want to include most fields, but want to exclude some fields based on their name, the function is similar. This function would include all fields except those with names that end with _layout
.
add_filter( 'relevanssi_index_custom_fields', 'rlv_no_layout_fields' ); function rlv_no_layout_fields( $custom_fields ) { return array_filter( $custom_fields, function( $field ) { return '_layout' !== substr( $field, -7 ); } ); }
If you have multiple endings you want to exclude:
add_filter( 'relevanssi_index_custom_fields', 'rlv_exclude_fields' ); function rlv_exclude_fields( $custom_fields ) { return array_filter( $custom_fields, function( $field ) { $exclude_suffixes = array( '_layout', '_image', '_embed' ); foreach ( $exclude_suffixes as $suffix ) { $suffix_length = strlen( $suffix ); if ( $suffix === substr( $field, -$suffix_length ) ) { return false; } } return true; } ); }