Roots Discourse

ACF Repeater content within a query loop - array map?

#1

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);

}

0 Likes

#3

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

0 Likes

#4

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.

0 Likes

#5

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

How to access content in a ACF relationship field within a repeater field