ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro) Repeater field has a specified sub field whose value matches the specified string.

We shall define a custom function that takes these 3 arguments so it can be used with Bricks‘ Dynamic data condition:

  • repeater name
  • sub field name
  • a string to check

This can also be applied to posts in a query loop.

Consider this scenario:

Game CPT having a Quote ACF Repeater type of field having these sub fields: Quote Amount (Number) and Quote Item (Select).

JSON export (you need to add a game CPT first)

“Game 1” entry when edited:

Requirement: When this “Game 1” post is being viewed on the front end, we want a Section that shows the Repeater data to be output only if, say, the Quote Item of “Bats” is present in at least 1 of the Repeater rows.

After implementing this tutorial:

When “Game 2” for which either the Quote Repeater field is empty or no row’s Quote Item has “Bats”, the Section won’t be output:

Step 1

Add the following in child theme‘s functions.php or a code snippets plugin:

<?php 

function bl_acf_repeater_has_sub_field_value( $repeater, $sub_field, $string ): bool {
    $found = false;

    if ( have_rows( $repeater ) ) {
        while( have_rows( $repeater ) ) {
            the_row();
            
            if ( $string === get_sub_field( $sub_field ) ) {
                $found = true;

                // there's a match, let's get outta here
                break;
            }
        } // end while have rows
    } // end if have rows

    return $found;
}

Step 2

Edit the single template of your CPT with Bricks.

Set up a Section that shows the Repeater data. Here’s a JSON export for copy pasting.

Apply this condition on the Section:

{echo:bl_acf_repeater_has_sub_field_value(quote,quote_item,Bats)}

where quote is the Repeater field name, quote_item is the select field name (in this example, could be any other type that returns text as well) and Bats is the value to check (make sure the case matches exactly).

Reference

https://support.advancedcustomfields.com/forums/topic/check-if-value-does-exist-in-any-repeater-subfield/#post-42846