Hiding Disabled ACF Flexible Content Layouts from Relevanssi Search Results

Implementing useful search results on your website is one of the best things you can do to optimize user experience.

Making your WordPress search usable is a requirement for your organization’s website, so using plugins like Relevanssi to improve your results makes such a difference. In the past, we’ve talked about how integrating Relevanssi with the Advanced Custom Fields (ACF) plugin requires tweaking, and we’ve got more to show you.

For example, in the past, you had to use a filter like relevanssi_index_custom_fields to hide unwanted ACF fields from Relevanssi search results. This required going into your functions.php file and manually adding the names of fields that shouldn’t be indexed.

With the release of Relevanssi Premium 2.20 / Free 4.18, a new field setting was added to allow easy control of whether an ACF field should be shown in results – making it incredibly easy to improve your search results.

Screenshot 2026 03 23 At 9.50.37 am

 

Similarly, with the launch of Advanced Custom Fields version 6.5, the developers added a feature to easily disable flexible content layouts without deleting the content in them. This makes it incredibly easy to save a page’s content for a future time without having to make unnecessary copies of the page or store extra backups.

Screenshot 2026 03 23 At 9.54.37 am

Problem: Displaying Hidden Content in Search Results

While these have both been great additions to excellent plugins, it brings up an unusual problem: users searching your site could be getting results from these hidden layouts.

Using the built-in setting to remove fields from the Relevanssi index unfortunately doesn’t work, as these layouts are disabled only on certain pages – and not everywhere throughout the site.

In order to solve this problem, we first need to understand how Relevanssi indexes the data in these custom fields and how ACF saves them in the database.

Relevanssi provides three options to index custom fields.

  • All: All custom fields associated with a post/page are indexed. This option isn’t ideal, since these ACF fields are often storing other types of data – background color choices, column sizes, etc.
  • Some: A list of custom fields has to be added, which can be cumbersome and prone to error.
  • Visible: Only fields that are visible in the post edit screen in the admin are indexed.

Typically, the Visible option is the correct choice to use; however, since the disabled layout fields are still visible in the post edit screen, they will still be indexed and displayed in search results.

Filtering Out Disabled Content from Relevanssi

In the database, ACF saves data within flexible content fields by creating a meta key based on the name of the layout, followed by the index number that appears in the admin.

For example, say you have a flexible content field named “sections” with a number of layouts in it. For the fourth layout in the field, ACF would create the meta key as “sections_3_{layout name}_{field name}” (3 because the index starts at 0). For disabled layouts, ACF also saves the index in a serialized array with a meta key of “_sections_layout_meta”.

Knowing this, we are able to update the relevanssi_index_custom_fields filter to remove these fields from the Relevanssi index.

add_filter( 'relevanssi_index_custom_fields', 'dgtlnk_exclude_hidden_flexible_content_fields', 10, 2 );
function dgtlnk_exclude_hidden_flexible_content_fields( $custom_fields, $post_id ) {
    // Get the '_sections_layout_meta' field value
    $layout_meta = get_post_meta( $post_id, '_sections_layout_meta', true );
    // Check if $layout_meta has a 'disabled' array
    if ( is_array( $layout_meta ) && ! empty( $layout_meta['disabled'] ) ) {
        // Prepare array to store partial meta keys for disabled fields
        $exclude_partials = array();
        // Loop through disabled array and push partial meta keys to $exclude_partials
        foreach ( $layout_meta['disabled'] as $section_id ) {
            array_push($exclude_partials, 'sections_' . $section_id . '_');
        }
    }
    if ( $exclude_partials ) {
        // If $exclude_partials exists, filter the $custom_fields array
        return array_filter(
            $custom_fields,
            function( $field ) use ( $exclude_partials ) {
                foreach ( $exclude_partials as $partial ) {
                    // Check if the field name contains the partial string
                    if ( str_starts_with( $field, $partial ) ) {
                        return false; // Exclude this field
                    }
                }
                return true; // Keep this field
            }
        );
    } else {
        // Else return all custom fields
        return $custom_fields;
    }
}

In this code, we get the field value for  _sections_layout_meta for a given post. We then check if the value is an array and if a “disabled” index exists in the array. If it does exist, we loop through it and create partial meta keys for fields within the disabled layout in the “sections_{disabled layout index}” format. By utilizing the array_filter() function, we exclude any fields whose meta key starts with the partial meta key format that we created earlier.

By understanding how data is saved and retrieved, you can easily manage what ACF data is displayed in Relevanssi search results on your website as well.

If you need help implementing a better search experience for your users or for any other digital marketing needs, reach out to us and we’d be more than happy to assist.