Support

Account

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

  • Thanks for your advice. I guess I was mistaken. I thought that get_the_title could be called outside the loop? Also, So does the loop need to be around EVERY custom field used? For example:

    <h2><?php the_field('text'); ?></h2>

    Does this need to be in a while loop?

    I placed the entire page in a while loop besides the wrapper elements and changed get_the_title to the_title. I also understand you recommend “removing all the ACF and extra coding for displaying custom field and data and to first get the template code correct to get the post that’s supposed to be displayed in order.”

    I guess I am confused because I thought I was accurately getting the correct post? My issue is checking to see if there is a relationship with like specialties. Again, thank you for your time, I am truly trying to code the correct and semantic way, I am just a little confused.

    	<?php 
    			$this_post = $post->ID;
    			$posts = get_field('doctor_specialties'); 
    		?>
    	
    
    		<?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() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    						'compare' => 'LIKE'
    					)
    				)
    			));
    		?>
    
    		<?php if( $doctors ): ?>
    		
    		<div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
    			
    			<div class="single-specialty-container">
    				
    				<?php if( $posts ): ?>
    				    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    				        <?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 endif; ?>
    					
    			
    				<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>
    				
    			</div>
    		</div>
    
    		<?php else: ?>
    
    		<!-- 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" style="background:#ffffff;">
    			
    			<h1> THIS DIV DISPLAYS IF THERE ARE NO OTHER DOCTORS WITH LIKE SPECIALTIES</h1>
    			
    		</div>
    
    		<?php endif; ?>
    
    		<?php wp_reset_postdata(); ?>

    Thanks