Support

Account

Home Forums Front-end Issues Querying with ACF relationship values Reply To: Querying with ACF relationship values

  • Hey John, Thank you for the quick response. I tried implementing the solution you suggested but have some sort of PHP error because I am getting a white screen on page load. Yes, I am trying to display the div tout-more-specialists and all of its content if the div has a relationship with more doctors.

    For Example:

    The Doctor has a specialty of Urology. I only need to show the div tout-more-specialists if there are other specialists that also specialize in Urology. If there are no other doctors specializing in Urology then display an alternate div with alternate content. I successfully was able to use the relationship fields to get all doctors that have the same specialty, but my issue is setting it up where if no one else has that specialty then display other content. Again any help is appreciated and thank you for getting back to me.

    Here is my updated code based on your suggestion. (The reason I am posting all of the code on the page is because I am not sure if it is causing other conflicts.)

    <?php get_header(); ?>
    			
    	<div class="row" data-equalizer="bridge">
    
    		<div class="large-4 medium-4 small-12 columns tout-single-doctor" data-equalizer-watch="bridge">
    			<div class="single-doctor-container">
    				<h2 class="doctor-title"><?php echo get_the_title(); ?></h2>
    					<?php 
    					$posts = get_field('doctor_specialties');
    					if( $posts ): ?>
    					    <?php foreach( $posts as $post): ?>
    					        <?php setup_postdata($post); ?>
    				            <a class="doctor-specialty-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    					    <?php endforeach; ?>
    					    <?php wp_reset_postdata(); ?>
    					<?php endif; ?>
    					
    					<?php the_post_thumbnail('full'); ?> 
    					
    					<?php 
    					$posts = get_field('doctor_services');
    					if( $posts ): ?>
    					    <?php foreach( $posts as $post): ?>
    					        <?php setup_postdata($post); ?>
    				            <p><span class="doctor-background-title">Provider Services:</span> <a class="doctor-specialty-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    					    <?php endforeach; ?>
    					    <?php wp_reset_postdata(); ?>
    					<?php endif; ?>
    
    					<?php if( get_field( "doctor_biography" ) ): ?>
    						<p><span class="doctor-background-title">Provider Biography</span> <?php the_field( "doctor_biography" ); ?></p>
    					<?php endif ?>
    
    				    <?php if( get_field( "doctor_education" ) ): ?>
    						<p><span class="doctor-background-title">Provider Education:</span> <?php the_field( "doctor_education" ); ?></p>
    					<?php endif ?>
    
    					<?php if( get_field( "doctor_residency" ) ): ?>
    						<p><span class="doctor-background-title">Provider Residency:</span> <?php the_field( "doctor_residency" ); ?></p>
    					<?php endif ?>
    
    					<?php if( get_field( "doctor_internship" ) ): ?>
    						<p><span class="doctor-background-title">Provider Internship:</span> <?php the_field( "doctor_internship" ); ?></p>
    					<?php endif ?>
    
    					<?php if( get_field( "doctor_fellowship" ) ): ?>
    						<p><span class="doctor-background-title">Provider Fellowship:</span> <?php the_field( "doctor_fellowship" ); ?></p>
    					<?php endif ?>
    
    					<?php if( get_field( "doctor_board_status" ) ): ?>
    						<p><span class="doctor-background-title">Board Status:</span> <?php the_field( "doctor_board_status" ); ?></p>
    					<?php endif ?>
    			</div><!-- end single-doctor-container -->
    		</div> <!-- end tout-single-doctor -->
    
    		<?php $this_post = $post->ID; ?>
    		<?php $posts = get_field('doctor_specialties');?>
    		<?php if( $posts ): ?>
    
    		  <div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
    		    <div class="single-specialty-container">
    		      
    		       <?php foreach( $posts as $post): ?>
    				        <?php setup_postdata($post); ?>
    			            <a class="doctor-specialty-link" href="<?php the_permalink(); ?>"><h2 class="related-specialties-title">More <?php the_title(); ?> Specialists</h2></a>    
    				    <?php endforeach; ?>
    				    <?php wp_reset_postdata(); ?>
    				<?php endif; ?>
    					
    
    				<?php while ( have_posts() ) : the_post(); ?>
    					<?php $doctors = get_posts(array(
    							'post_type' => 'our-providers',
    							'orderby' => 'meta_value',
    							'post__not_in' => array($this_post),
    							'meta_query' => array(
    								array(
    									'key' => 'doctor_specialties', // name of custom field
    									'value' => '"' . get_the_ID() . '"',
    									'compare' => 'LIKE'
    								)
    							)
    						));
    
    						?>
    						<?php if( $doctors ): ?>
    							<ul>
    							<?php foreach( $doctors as $doctor ): ?>
    								<li>
    									<a href="<?php echo get_permalink( $doctor->ID ); ?>">
    										<?php echo get_the_title( $doctor->ID ); ?>
    									</a>
    								</li>
    							<?php endforeach; ?>
    							</ul>
    						<?php endif; ?>
    
    					<?php endwhile; // end of the loop. ?>
    		    </div>
    		</div>
    		
    		<?php else: ?>
    		
    		<?php wp_reset_postdata(); ?>
    		  
    		<!-- If not providers with same specialty then display this div -->
    		<div class="large-4 medium-4 small-12 columns if-not-specialists" data-equalizer-watch="bridge">
    			
    			<!-- No Related Providers Content Here -->
    			
    		</div>
    
    		<?php endif; ?>
    
    		<div class="large-4 medium-4 small-12 columns tout-bridge-make-reservations_home" data-equalizer-watch="bridge">
    			<!-- Make Reservations Widget -->
    			<?php get_template_part( 'partials/global-custom-fields/make', 'reservations' ); ?>
    		</div>
    	
    	</div>
    <?php get_footer(); ?>