ACF Repeater content within a query loop - array map?

I’ve got the following code working fine to pullback ACF fields for a custom loop. However I’m not sure how to register the ACF repeater fields in a useful way to use it in my view. I have the current code in app.php which I’m then using in a partial view called property-loop.blade.php. Everything works fine apart from the ACF repeater:

Alternatively - does anyone have a theme / project that I could have a copy of to use as a resource only - I feel I’ve quite a few stupid blade and sage related questions that I could easily diagnose myself if I can see some working best practises. Happy to sign a disclaimer or donate towards Sage for the privilege.

public function propertyLoop() { $query = get_posts([ 'post_type' => 'property', 'posts_per_page'=> -1, ]);
  return array_map(function ($post) {
      

      return [


          'bathrooms'     =>      get_field('property_bathrooms', $post),
          'sleeps'        =>      get_field('property_sleeps', $post),
          'bedrooms'      =>      get_field('property_bedrooms', $post),
          'title'         =>      apply_filters('the_title', $post->post_title),
          'introduction'  =>      get_field('property_introduction'),
          'link'          =>      get_permalink($post->ID),

// ACF REPEATER for ‘property_gallery’
have_rows
get_post_meta
nested array map?

      ];


  }, $query);

}

Or maybe I’m totally trying to access custom loops in the wrong way?

It’s hard to say, I would need to see what you’re trying to accomplish. It would help to see the repo and set up on my side. There is however no reason that the acf-repeater field should not work in this case.

Unless I’m misunderstanding, IMO a nested array_map would be the way to go. IIRC ACF repeater fields just return an array, so it should be as simple as this:

public function propertyLoop() { 
  return array_map(function ($post) {
      return [
          'bathrooms'     =>      get_field('property_bathrooms', $post),
          'sleeps'        =>      get_field('property_sleeps', $post),
          'bedrooms'      =>      get_field('property_bedrooms', $post),
          'title'         =>      apply_filters('the_title', $post->post_title),
          'introduction'  =>      get_field('property_introduction'),
          'link'          =>      get_permalink($post->ID),
          'gallery'       =>      array_map(function($image) {
             return wp_get_attachment_image($image); // Or however you want to process this
          }, get_field('property_gallery', $post)),
      ];
  }, get_posts([ 'post_type' => 'property', 'posts_per_page'=> -1, ]));
}
1 Like

Hello @alwaysblank i am using sage 9 with ACF pro. I have created a section called testimonials (repeater field) and loaded it in a flexible ACF section. The testimonials contain an image and text field. I am unable to retrieve this in the controller.
My controller looks as follows:

public function getTestimonials()
      {
        // check if the flexible content field has rows of data
        if( have_rows('sections') ):

          // loop through the rows of data
          while ( have_rows('sections') ) : the_row();

            // check if the nested repeater field has rows of data
            if( have_rows('section:_testimonials') ):

              //echo '<ul>';

              // loop through the rows of data
              while ( have_rows('section:_testimonials') ) : the_row();

              $coder_info = array ('name' => get_sub_field('coder_name'),
                                  'image' => get_sub_field('coder_image'),
                                  'country' => get_sub_field('coder_country'),
                                  'experience' => get_sub_field('coder_experience_with_codersclan'),
                                  'message' => get_sub_field('coder_testimonial_message')
                                  );

              //echo '<li>"' . $name . '"</li>';

            endwhile;

            //echo '</ul>';

          endif;

      endwhile;

      return $coder_info;

      else :

        $coder_info = "";

        return $coder_info;

      endif;
    }

and the blade template is as follows:

<section class="section section-testimonials">
    <div class="container">
      @if($get_testimonials)
      <ul>
        @foreach($get_testimonials as $r)
        <li>{!! $r['name'] !!}</li>
        @endforeach
      </ul>
      @else
      <p>Doesnt work!</p>
      @endif
    </div>
</section>

Nothing seems to work, kindly assist.

This is overwriting your $coder_info variable every time it loops, not appending it (which I assume would be the intended behavior). This means you return an array, but not the array you expect, so $r['name'] doesn’t exist. This is a basic PHP issue though, nothing to do with Blade, Controllers, or ACF. I’m also not sure why you posted this in a year-old thread when your implementation and question seem only tangentially related. Generally it’s more helpful to create a new thread, with detailed information about the problem you’re trying to solve, what you’ve already attempted, and how things are currently failing, instead of “Nothing seems to work”.

Hi @alwaysblank I have managed to resolve, the problem is exactly what you said, $coder_info gets overwritten during each loop. I have fixed by adding[] to make it $coder_info[] and it now works.

Thank you.