Home › Forums › Front-end Issues › Conditional statement for relationship field? › Reply To: Conditional statement for relationship field?
You should check if you have testimonials ids
<?php
// get only first 1 results
$ids = get_field('testimonials_relationship', false, false);
// check if have ids
if($ids) {
$the_query = new WP_Query(array(
'post_type' => 'testimonials',
'posts_per_page' => 1,
'post__in' => $ids,
'post_status' => 'any',
'orderby' => 'rand',
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
}
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.