New to Blade: Issue with Array Map and CPT

There’s nothing special about ACF fields. They’re just fields on a post object. Here’s a very minimal example that should work:

// this is the controller
// Archive.php
public function SomePosts()
{
 if ($wp_query->post_count > 0) {
     return array_map(function ($post) {
         return [ 
            'field_one' => get_field('an_acf_field', $post->ID) ,
            'field_two' => get_field('another_acf_field', $post->ID) ,
         ];
     }, $wp_query->posts);
 }
 return false;
}
// this is the blade
// archive.blade.php

// it is import here to **not** use $posts because that would try to
// overwrite the $posts global that WordPress archives use, and that
// is kind of asking for trouble; hence $some_posts.

@if($some_posts)
   // it is important here to **not** use $post because that would try
   // and overwrite the global $post that WordPress uses, and that kind
   // is asking for trouble, hence $this_post.
   @foreach($some_posts as $this_post)
      <h1>{{ $this_post['field_one'] }}</h1>
      <p>{{ $this_post['field_two'] }}</p>
   @endforeach
@endif
1 Like