Home Forums ACF PRO Can we use get_field() for ACF Group's subfields?


Can we use get_field() for ACF Group's subfields?

  • Thanks for the cool plugin.

    Last time I use ACF Group a lot.

    For example, we have a Group ‘footer’ with a Text subfield ‘copyright’. So, we can use:
    $footer = get_field('footer'); $copyright = !empty($footer['copyright']) ? $footer['copyright'] : 'a default value';
    or to use:
    have_rows('footer'); the_row(); $copyright = get_sub_field('copyright');

    But I know that the copyright is saved as ‘footer_copyright’ and if I use $copyright = get_field('footer_copyright'); I will have a right value.

    Can I use get_field() for a first-level any type subfield like a usual standard way or no?
    (The context: ACF Group is useful for some ‘namespaces’, but it requires more PHP code).

  • You may already have your answer

    It sounds like you’ve already tried

    $copyright = get_field('footer_copyright');

    and that is is working for you.

    It makes sense that it would work for any sub field where you know the exact meta key where it is stored, in the same way that get_post_meta() will work if you know the right meta_key to use to get the sub field.

  • Yes, I tried and it works. But is it an ACF feature or temporary luck? I mean if I will use this method could I think that ACF going to work that way long time in the future?

    $group_subfield_in_any_times_on_future = get_field($group_name . '_' . $subfield_name);

    Or does it fraught with problems in the future (some breaking changes)?

  • I don’t see any reason why this should stop working, it’s really a side effect of how ACF works. I don’t see ACF changing the way it works any time so I don’t see why this would stop working.

  • Just want to say, maybe will better to add some explanation on the ACF Group documentation page. “You can use get_gield() in [this way] for sub-fields etc”.

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

The topic ‘Can we use get_field() for ACF Group's subfields?’ is closed to new replies.