Support

Account

Home Forums Front-end Issues Conditional statement for relationship field?

Solved

Conditional statement for relationship field?

  • I have a relationship field (testimonials) that i’d like to show one testimonial on each page. I’d like the user to be able to select up to three testimonials (but only show one of those at random on page load) and also have the option to not display any at all (if they leave the field blank). The following code works as long as the relationship field is not blank. If I don’t select any testimonials in the field, then it displays all of them. What am I missing here? Is there a way to hide the field if nothing is selected?

    <?php 		
    		// get only first 1 results
    		$ids = get_field('testimonials_relationship', false, false);
    		
    		$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; ?>
    
  • 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; ?>
    
    }
    
    
  • Hmm, that just breaks the page. This syntax doesn’t seem correct. I don’t know enough PHP to troubleshoot!

  • Sorry, I forgot to open and close the last php tag

    
    <?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; ?>
    
    <?php } ?>
    
    
  • Ah yes that worked! I was just playing around with it and it seems like the following works too. Are they both valid ways of writing the code, or is one better syntax than the other?

    
    <?php 		
    	// get only first 1 results
    	$ids = get_field('testimonials_relationship', false, false);			
    	
    	$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() ) : ?>				
    	
    		<!-- the loop -->
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		
    			<?php
    				// Must be inside a loop.							 
    				// If there are testimonials chosen display:
    				if ($ids) { ?>
    					
    					<h2><?php the_title(); ?></h2>
    					
    				<?php } else  { ?>							    
    					<!-- No testimonials for this page -->
    				<?php } ?>
    			
    		<?php endwhile; ?>
    		<!-- end of the loop -->
    	    
    		<?php wp_reset_postdata(); ?>
    	
    	<?php else : ?>
    		<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
    	<?php endif; ?>
    
  • The principal difference is your code executes a query, wich if the $ids is empty the query with the argument “post__in” will be empty, so retrieves all of the testimonials posts, and then checks if you have $ids or not. So this does a call to the database and then checks for the $ids.

    In my code checks if you have $ids or not, if you don’t have $ids then don’t do the query (which means don’t do a call to the database)

    I hope this helps you.

  • Yes, that makes sense. Thank you for your help!

  • Hello,

    thanks for the code, that is really help me a lot, but still i got a issue in this code, when i add relationship field from admin side then if i select in the only 2 Post From My CPT , then still it displayed all. i just want display only which is i selected from admin side

    please help me if you can????

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Conditional statement for relationship field?’ is closed to new replies.