Home Forums General Issues Particular reationship query Reply To: Particular reationship 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