Search code examples
phpwordpressrepeatercustom-fieldsacfpro

Get custom post type fields through a repeater


I'm in a WP project. I created a custom post type called Team Members I also created a custom field block with a field called contact_members which is a repeater and has two sub-fields called location_map and member. This member sub-field is related with the custom post type team member.

I have no issues with getting the location_map. The issue is that I can't get the post type fields. (coming from contact_members->member->post type

  <?php
    $members = get_field('contact_members');
  ?>
  @foreach($members as $member)
    <div class="member {{ $member['location_map'] }}">
      <img class="map" src="{{ get_the_post_thumbnail_url($member['member']->ID) }}">
      <h3>{{ get_the_title($member['member']->ID) }}</h3>
      <p class="position">{{ $member['member']->position }}</p>
      <p class="location">{{ $member['member']->location }}</p>
      <a href="#">Contact</a>
    </div>
  @endforeach

Solution

  • I found the answer, I should put [0] after $member['member'], in order to reach the array's first element and after that I can finally go after what I want from the object, so:

    <?php $members = get_field('contact_members'); ?>
    @foreach($members as $member)
        <div class="member {{ $member['location_map'] }}">
            <img class="member-image" src="{{ get_the_post_thumbnail_url($member['member'][0]->ID) }}">
            <h3 class="member-name">{{ $member['member'][0]->post_title }}</h3>
            <p class="position">{{ $member['member'][0]->position }}</p>
            <p class="location">{{ $member['member'][0]->location }}</p>
            <a href="#">Contact</a>
        </div>
    @endforeach