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
<?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
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.