Support

Account

Home Forums General Issues Particular reationship query

Solved

Particular reationship query

  • Hi,
    I follow this guide
    https://www.advancedcustomfields.com/resources/querying-relationship-fields/
    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?

    Thanks

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

    
    <?php 
    
    //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(
    		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>
    </a> 
    </div>
    	<?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)

The topic ‘Particular reationship query’ is closed to new replies.