Home Forums General Issues Particular reationship query


Particular reationship query

  • Hi,
    I follow this guide
    It’ all ok !
    But I need to another relationship query that is not explained in the example.
    Remaining in terms of the example, I would like that in single-doctor.php (in addition to viewing the doctor’s locations) you can see the list of doctors working in the same location.
    Example in single-doctor.php:

    Doctor 5 works to Melbourne.
    In Melbourne work too: Doctor 1, Doctor 3.

    what’s the right query?


  • Auto Solved 🙂
    This is the code that can serve others if they have the same problem

    //take relationship field of this doctor
    $location= get_field('location');
     if( $location):
     foreach( $location as $l ):
    //array of post type doctor and take the post with key location and compare with the ID of location of this doctor $l->ID
    		$doctors = get_posts(array(
    		'post_type' => 'doctor',
    		'posts_per_page' => '-1',
    		'post__not_in' => [get_queried_object_id()],
    		'meta_query' => array(
    		'key' => 'location', // name of custom field
    		'value' => $l->ID, // the ID of this doctor
    		'compare' => 'LIKE'
    	<?php if( $doctors ): ?>
    	<?php foreach( $doctors as $doctor ): ?>
    <div class="col-md-2">						
     <a href="<?php echo get_permalink( $doctor->ID ); ?>">
       <img src="<?php echo get_the_post_thumbnail_url( $doctor->ID ); ?>">
       <h2><?php echo get_the_title( $doctor->ID ); ?></h2>
    	<?php endforeach; ?> //second foreach
    	<?php endif; ?> //second if
    	<?php endforeach; ?> //first foreach
    	<?php endif; ?> //first if
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.