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

ACF Checkbox Items with Custom SVG Bullets in Bricks

ACF Checkbox Items with Custom SVG Bullets in Bricks

We walk through outputting list items with SVG icons based on the choice of a ACF Checkbox field.
Categories:
Tags:
Modifying ACF Field Value Before It Is Output

Modifying ACF Field Value Before It Is Output

Consider the scenario where a Page has a "Page Fields" field group created with ACF Pro like this: Field name: scientific_coordinators (type Repeater)|__ Sub field…
Categories:
Pro
Sorting ACF Relationship Posts by Date Meta in Bricks

Sorting ACF Relationship Posts by Date Meta in Bricks

Consider the following setup: CPT: Events ACF Fields:Event Date Information (Group)|__Event Date (Date Picker)Pick Sub Events (Relationship) An Event can have 1 or more related…
Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
Categories:
How to conditionally load your scripts based on an ACF field

How to conditionally load your scripts based on an ACF field

This tutorial provides the PHP codes that can be pasted in order to enqueue your scripts conditionally based on the value of an ACF field…
Categories:
Pro
ACF Relationship Select Filter in Bricks

ACF Relationship Select Filter in Bricks

In the BricksLabs Facebook group, a user asks: I'm learning about ACF relationships and attempting to output a list of posts on any given page,…
Categories: