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 your site 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.

4 comments ACF: Field names appearing in excerpts

      1. To use this filter function on a sub field, or a field nested inside a Flexible Content field, what might the $unwanted_fields line item look like to target the sub field, or field nested in Flexible Content?

        1. The best way to find out is to look at the wp_postmeta database table. You can find the names of the fields that contain the field layouts. But if you have “flexible_content_1_sub_field”, the layout is in “flexible_content”. Based on that, if you have “flexible_content_1_flexible_sub_content_1_sub_field”, you probably need to include “flexible_content_1_flexible_sub_content”. But check the database table, that’s the best way to find out.

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.