Support

Account

Home Forums General Issues Directly get value in group sub-field

Solved

Directly get value in group sub-field

    • guzart

    • February 19, 2021 at 12:01 pm

    Hello there,

    Is there a way to directly get the value of a group sub-field.

    For example, I have a Group field my_options (field_abcde), and inside that field I have an title field (field_12345).

    I tried using get_field(field_12345, 'option') in order to get the title value directly, but that’s not working. It seems that the values are only saved in the “root” field.

    I know there are simpler workarounds, but my real use case is more complicated. But if you are curious about my real use case:

    —–

    I want to build a custom ACF Field that will let admin’s have a “select” field that’s populated with all the repeater type fields in the options field group. For example, an Admin creates a list of approved images using a repeater field in the “Site Options” page, then using this custom ACF Field it adds an image field to a block field group, so that Editors can add a block in the admin and choose from the approved list of images.

    Thank you!

  • Hi Guzart!

    You can get your sub field like this:

    First of all, get your group of fields
    $group_values = get_field('group_name'); // use get_field('group_name', 'options') if your field is in a global page options
    (‘option’) work too

    Now you have your fields, display your title with $group_values['title']

    Cheers
    Sam

  • 
    $value = get_field('{GROUP FIELD NAME}_{SUB FIELD NAME}');
    
    • guzart

    • February 22, 2021 at 7:28 am

    Thank you both for your help. Using the field names concatenated with an _ worked great.

    Unfortunately, I am going to stick with the more inefficient method of fetching the group value and traversing into the individual value using loops, since I feel more comfortable storing ‘field keys’ since they are less likely to change than ‘field names’.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.