Posted on

ACF: Field names appearing in excerpts

With Advanced Custom Fields, sometimes Relevanssi picks up custom field names to include in the excerpts.

This problem happens when you set Relevanssi to index all visible custom fields and have Flexible Content fields. Let’s assume there’s a Flexible Content field with the name flexible_content. It has a layout named field_layout, and in that layout, there are fields with the name flexible_field.

In this case, the content is in the custom field flexible_content_0_flexible_field (with increasing numbers for each new field). ACF, however, also creates a field flexible_content, which has an array with the layouts listed (at least that’s what I think it has). Since that too is a visible field, Relevanssi indexes it and uses it for excerpts. Thus the layout names appear in excerpts.

The solution to this problem is to remove the root field, flexible_content, from the Relevanssi index and excerpts. That is a straightforward thing to do with a filter function:

add_filter( 'relevanssi_index_custom_fields', 'rlv_remove_unwanted' );
function rlv_remove_unwanted( $fields ) {
    $unwanted_fields[] = 'flexible_content';
    return array_diff( $fields, $unwanted_fields );
}

Add this function to the theme functions.php file and rebuild the index. Now the root field is no longer indexed or used in excerpts.

Another approach is to stop using “Visible” in the custom field indexing settings and instead specify the exact fields you want Relevanssi to index. Still, with lots of Flexible Content, that can be complicated.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.