-1

I am trying to use ACF Relationships to associate members to different committees. On the front end, I want to have the committee name and the board members that are associated with them. I have attempted to follow the ACF Relationship guide, several stack answers, and other stuff, but am pretty new to WP so I have been unsuccessful.

Here are screenshots of my Wordpress setup enter image description here enter image description here

Here is my code:

    <?php
        $args = array('post_type' => 'committees');
        $the_query = new WP_Query($args);
        if (have_posts()): 
        while ($the_query->have_posts()): $the_query->the_post();
    ?>
    <li style='margin-bottom: 15px;'>
        <h5 style='margin: 0;'><?php echo the_field("committee_name"); ?></h5>
        <ul>
            <li>associated committee members repeated here</li>
        </ul>
    </li>
    <?php 
        endwhile;
        endif;
        wp_reset_postdata();
    ?>

Edits

<?php
    $args = array('post_type' => 'committees');
    $the_query = new WP_Query($args);
    if (have_posts()): 
    while ($the_query->have_posts()): $the_query->the_post();
    $members = get_field('committee_members', $member->ID);
?>
<li style='margin-bottom: 15px;'>
    <h5 style='margin: 0;'><?php echo the_field("committee_name"); ?></h5>
    <ul>
        <?php foreach($members as $member)
            echo "<li>";
            echo $member->post_title;
            echo "</li>";
        ?>
    </ul>
</li>
<?php 
    endwhile;
    endif;
    wp_reset_postdata();
?>

enter image description here enter image description here

  • You mentioned your issue, but didn't mention an error, or a specific question. What's not working? – gdaniel May 29 '15 at 21:41
  • Whenever I try to use something like foreach( $posts as $post): to iterate over the data, the php always breaks the page. All I really want is to be able to show the association under the committee. – alexmattorr May 29 '15 at 21:47
0

Relationship fields can either return an Objects or a Post IDs. That's up to you (you can choose it when creating the field). Either way that object or ID would be returned inside an array, because the field gives you the option to select multiple values.

So in your loop:

$members = get_field('committee_members'); // assuming that's the field slug

//if it's returning the object

foreach($members as $member){

  echo $member->post_title; //member name

}

That would assign all Committee Members to $members. Then you just need to loop through all $members and print their name, or anything else you need that's related to that post. If the Committee Member also has ACF custom fields you can access it by using:

get_field("YOU_FIELD_NAME", $member->ID);
  • I updated the code by your reference, and it works but is only outputting the last member from the first committee onto all committees. I am adding a screenshot to a edit above. – alexmattorr May 29 '15 at 22:08
  • Remove $member->ID from get_field – gdaniel May 29 '15 at 22:41
  • I just updated it, and they last of that specific committee was still just showing up. I put in a arrow to see if it was iterating through them, and a arrow with the correct amount of people is being output but not the member name itself. Screenshot added in edits for reference. – alexmattorr May 29 '15 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.