ACF Repeater Sub Field Data

Looking to get a specific sub field’s data like its label from a specific ACF Repeater?

Here’s a function for that:

<?php 

if ( ! function_exists( 'bl_get_acf_sub_field_object' ) ) {
    function bl_get_acf_sub_field_object( string $field_name, string $repeater ): array {
        $object = [];

        if ( have_rows( $repeater ) ):
            while( have_rows( $repeater ) ): the_row();
                $field_object = get_sub_field_object( $field_name );

                if ( $field_object ) {
                    $object = $field_object;
                }
            endwhile;
        endif;
        
        return $object;
    }
}

Given

Sample usage:

echo bl_get_acf_sub_field_object( 'institution', 'scientific_coordinators' )['label']

(for non-array values)

The list of possible array keys besides label can be found in the doc here.

If you use Bricks, to show the label and the value in the Repeater’s query loop:

The benefit of doing this over hardcoding label text within the Basic Text element’s text, should you need to display the labels, is that now the output will always be in sync with any changes done to the labels in the field group.

Remember that it is not possible to use :array_value Bricks filter for outputting labels without a custom function because ACF Repeater query loop object only contains name and value.

Thanks to Jenn Lee from the Bricks support team for the code snippet.

Instant access to 390+ Bricks code tutorials with BricksLabs Pro

Leave the first comment