Home Forums Front-end Issues Get post ID from relationship field for wp_query


Get post ID from relationship field for wp_query

  • I’m working on a project that is using ACF and everything is going wonderfully, except I think I need a small push in the right direction to solve something I’ve been struggling with for the last day.

    I have a CPT called “Projects” and in the field group I have an optional Relationship field where the user can select a person from the “People” CPT. If the user selects a person, it appears easily with a simple the_field. This is awesome!

    Here’s where I’m stuck…

    If there is a Project Manager selected, the template needs to display data from another field group as inputted in the “People CPT.” Each People CPT post has a number of ACF fields that display their title, location, etc.

    What I’m having a hard time wrapping my head around is this: Is is possible to grab the post id for the selected Person? And if so, I feel like the get_field_object function is what I’d like to use, but in my searching I haven’t quite found a solution that makes sense.

    I’m currently using a simple wp_query loop to pull in the People CPT, but it needs to pull in the specific person. I feel like I’m right on the cusp of this, but I’m unsure how to say, “get the post id from this person as selected in Projects and display the custom fields of my choosing from that post.

          $queryObject = new WP_Query( 'post_type=people&posts_per_page=1' );
          // The Loop!
          if ($queryObject->have_posts()) {
            while ($queryObject->have_posts()) {
              $queryObject->the_post(); ?>

    Does this make sense? I’m not looking for someone to solve this, but more a push in the right direction. Any help is much appreciated!

    Also, it’s possible that I did a terrible job of explaining this, so let me know if I can do better.

    Thanks again for a great plugin and awesome support here!

  • Hi @velograph

    From the relationship field, you can get the selected people. Then you can loop through these people and render what ever info you would like, this can include a custom field value from that person. Here is an example:

    // get selected people
    $people = get_field('relationship_field_name');
    if( $people )
    	foreach( $people as $person )
    		<h3><?php echo get_the_title( $person->ID ); ?></h3>
    		<p>Custom field from person: <?php the_field('field_name', $person); ?>


  • Thanks for the help there Elliot. After throwing this in the loop, I’ve got exactly what I’m looking for.

    On to the next task!

  • Im trying to do the exact same thing with one simple modification to make the main loop ORDER_BY ASC a custom field value in the related post called “artist_last_name”.

    Elliot – can you advise how to modify this to get the main loop to order by a value in the related post’s custom field?

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Get post ID from relationship field for wp_query’ is closed to new replies.