15th Dec '23
/
0 comments

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.

Get access to all 610 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Querying Posts by Date/Datetime Custom Fields in Bricks

Querying Posts by Date/Datetime Custom Fields in Bricks

This Pro tutorial shows how to configure ACF and Meta Box to query posts in Bricks by custom field(s) of the type Date/Date Time Picker…
Categories:
Tags:
Pro
Events grouped by Event Date custom field in Bricks

Events grouped by Event Date custom field in Bricks

This Pro tutorial for Bricks users provides the steps to output posts of an event custom post type dynamically grouped by the value of their…
Categories:
Pro
Programmatically populating ACF field values in WordPress

Programmatically populating ACF field values in WordPress

An example of how to set the values of a Select-type field with the names and labels of all public post types.
Categories:
Pro
ACF Image Data in Bricks

ACF Image Data in Bricks

Displaying Alt text, Caption, Title and URL of an ACF image field for posts in a query loop.
Categories:
Tags:
Pro
ACF Checkbox Sub Field as Unordered List in Bricks

ACF Checkbox Sub Field as Unordered List in Bricks

Updated on 22 Sep 2023 This Pro tutorial shows how the checked options of a Checkbox-type of Sub Field inside a ACF Repeater field can…
Categories: