Support

Account

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; ?>
    
    }