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've just released ACF 5.12.3 with a security fix to prevent arbitrary file uploads to forms with ACF fields.
— Advanced Custom Fields (@wp_acf) July 14, 2022
Now that we've released this update, we recommend updating your sites as soon as possible.
© 2022 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.