Display ACF repeater sub field with array_map, showing no output

Have tried several methods posted to show data from ACF repeater sub fields, but so far none have displayed anything for an empty area within ""s.
Shouldn’t this be search through the field and outputting the sub field data?

    // active projects loop
public function activeprojectsLoop() {
    $activeprojects = get_posts([
        'post_type' => 'active_project',
        'posts_per_page' => '8',
    ]);

    return array_map(function ($post) {
        return [
            'title' => apply_filters('the_title', $post->post_title),
            'pathitem' => get_sub_field('ap_milestone_title',$post),
        ];
    }, $activeprojects);
}

blade template:

                @foreach($activeprojects_loop as $activeproject)
                  {!! $activeproject['pathitem'] !!}
                @endforeach

From the documentation for get_sub_field():

This function will return a sub field value from a repeater field or flexible content field loop. This function is used within a have_rows() loop.

Your code is not calling get_sub_field() from within a repeater field or flexible content loop, so it has no context that tells it what field it is getting a sub field of. The loop you have is a loop of posts in the active_project post type, not a repeater field or flexible content loop.

Well that definitely clears up my newb train of thought so far.

How are acf repeater sub fields called in a vanilla kind of way? arrap_map on the pathitem?

The documentation linked in my post above explains how sub fields are called. I’m not sure I understand your question.

Apologies for the confusion. This framework is awesome but the feeling of being overwhelmed with nesting fields logic is growing.

I have an acf repeater, ‘ap_project_critical_path_item’

In that is the sub field is a text item row, ‘ap_milestone_title’

How can I build a loop to show those repeater items?

From the documentation you kindly posted, shouldn’t I be able to change my above function to something like this:

public function activeprojectsLoop() {
    $activeprojects = get_posts([
        'post_type' => 'active_project',
        'posts_per_page' => '8',
    ]);

    return array_map(function ($post) {
        return [
            'title' => apply_filters('the_title', $post->post_title),
            'pathitem' => get_field('ap_project_critical_path_item', function(){
return array_map(function(pathitem) {
'pathTitle' => get_sub_field('ap_milestone_title')});
}

$post),
        ];
    }, $activeprojects);
}

Or am I missing some pretty important details?

I can’t really parse the code you’ve posted, I think there are some errors in it.

get_field('name-of-repeater-field') will return an associative array when you call it on a repeater field; You can just iterate over that array the way you’d iterative over any other array. AFAIK get_sub_field() is only useful if you’re using ACF’s have_rows() loop (as mentioned in the documentation), which you don’t seem to be doing. Personally I never use get_sub_field().

1 Like

Thanks again for trying to read that spaghetti. Long day. You gave me the hints to get back on track!

Found my answer here: Advanced custom fields variables using get_sub_field not working

Turns out it’s 6 lines of code and so much simpler than I first thought. Definitely went down the wrong rabbit hole.

This topic was automatically closed after 42 days. New replies are no longer allowed.