Support

Account

Home Forums Front-end Issues Querying with ACF relationship values

Solved

Querying with ACF relationship values

  • Hello everyone, I am fairly new to WordPress and I am having a small issue. I was able to successfully query the ACF Relationship fields. My only issue is I need to display alternate content if there are no items to display. For example, I am getting all doctor names that have the same specialty, if there are no doctors with that same specialty, I need to display a different content block or widget in place of the tout-more-specialists div. Any help would be appreciated, I feel like I am really close but just don’t have the best PHP skills yet.

    Thanks,

    Jameson

    <?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 -->
    
    		<div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
    			<div class="single-specialty-container">
    				<?php 
    				$this_post = $post->ID;
    				$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(); ?>"><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>
    		<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(); ?>
  • I’m going to pare down your code just a bit. If I understand you want to show the tout-more-specialists div, including everything in it, only if there are posts returned here $posts = get_field('doctor_specialties'); and show something else if not.

    
    // move check for posts before opening div tag
    $this_post = $post->ID;
    $posts = get_field('doctor_specialties');
    if( $posts ): ?>
      <div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
        <div class="single-specialty-container">
          <!-- rest of div content here -->
        </div>
    </div>
    <?php 
    // add and else after the closing div
    else: ?>
      // you might need to do a reset here
      wp_reset_postdata();
      // put code to display something else here
    
    endif;
    
  • 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(); ?>
  • ANOTHER UPDATE

    I was able to get the div to display alternate content with the if else however now the doctors that do have relationships don’t display. Instead the alternate div is always displayed.

    <?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 ): ?>
    		<div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
    			<div class="single-specialty-container">
    				<?php 
    				$this_post = $post->ID;
    				$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(); ?>"><h2 class="related-specialties-title">More <?php the_title(); ?> Specialists</h2></a>    
    				    <?php endforeach; ?>
    				    <?php wp_reset_postdata(); ?>
    				<?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">
    			
    			<!-- No Related Providers Content Here -->
    			
    		</div>
    		<?php endif; ?>
    		<?php wp_reset_postdata(); ?>
    		<?php endwhile; // end of the loop. ?>
  • I was just looking over the original code that you posted and it contains some serious errors and logic flow issues.

    For example, near to top at about line 7 there is:

    <h2 class="doctor-title"><?php echo get_the_title(); ?></h2>

    This should appear inside the main loop that is displaying the current post, however the main loop does not happen until deep into the code for this template at this line.

    <?php while ( have_posts() ) : the_post(); ?>

    This also means that most all of the get_field() statements that exist before the main WP loop starts should be returning nothing or erroneous data.

    My suggestion would be to remove 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. Then once that’s done to start adding in extra data.

    Basically, except for any wrapper elements, everything for displaying this post should fall between.

    <?php while ( have_posts() ) : the_post(); ?>

    AND

    <?php endwhile; // end of the loop. ?>

  • 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

  • My last code post did not work, I see what you are saying that wrapping my page in the while loop is the first step, I wonder what I need to change to display the results from this point, because now no specialties are displaying even for the doctors that have multiple relationships.

  • Let’s go back to the code you originally posted. I’ve cleaned it up a bit. Don’t get me wrong, but the code style used makes my eyes bleed, it’s all the opening and closing PHP tags. I’ve also swapped out the alternate php syntax with bracket style because it allows me to collapse code.

    I’ve added notes where I see the troubles

    
    <?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">
        
        <!-- 
              All of the code inside this div
              class tout-single-doctor should be
              in The Loop
        -->
        
        <div class="single-doctor-container">
          <h2 class="doctor-title"><?php 
            
            // to use get_the_title outside of the loop
            // it requires a post ID
            // assuming that you want to get the current
            // post title, this should be inside The Loop
            echo get_the_title(); ?>
          </h2>
          <?php 
            
            // the following function call and loop should be
            // inside The Loop
            $posts = get_field('doctor_specialties');
            if ($posts) {
              foreach ($posts as $post) {
                setup_postdata($post);
                ?>
                  <a class="doctor-specialty-link" href="<?php 
                      the_permalink(); ?>"><?php the_title(); ?></a>
                <?php 
              } // end foreach $posts
              wp_reset_postdata(); 
            } // end if $posts
            
            // the next function call should be
            // in the loop
            the_post_thumbnail('full');
            
            // the next function call and loop
            // should be in The Loop
            $posts = get_field('doctor_services');
            if ($posts) {
              foreach ($posts as $post) {
                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 
              } // end foreach $posts
              wp_reset_postdata();
            } // end if $posts
            
            // cleaned this up a bit to reduce code
            // but all of the get_field() calls
            // need to be in The Loop
            $fields = array(
              'doctor_biography' => 'Provider Biography',
              'doctor_education' => 'Provider Education',
              'doctor_residency' => 'Provider Residency',
              'doctor_internship' => 'Provider Internship',
              'doctor_fellowship' => 'Provider Fellowship',
              'doctor_board_status' => 'Board Status'
            );
            foreach ($fields as $field => $label) {
              $value = get_field($field);
              if ($value) {
                ?>
                  <p>
                    <span class="doctor-background-title"><?php 
                        echo $label; ?>:</span>
                    <?php echo $value; ?>
                  </p>
                <?php 
              } // end if $value
            } // end foreach $fields
          ?>
          
        </div><!-- end single-doctor-container -->
      </div> <!-- end tout-single-doctor -->
    
      <div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
        <div class="single-specialty-container">
          <?php 
            
            // I don't know what $post-ID is returning
            // this needs to be inside The Loop
            $this_post = $post->ID;
            
            // another call to get_field()
            // that should be in The Loop
            $posts = get_field('doctor_specialties');
            if ($posts) {
              foreach ($posts as $post) {
                setup_postdata($post);
                ?>
                  <h2 class="related-specialties-title">
                    <a class="doctor-specialty-link" href="<?php 
                        the_permalink(); ?>">More <?php 
                        the_title(); ?> Specialists</a></h2>
                <?php 
              } // end foreach $posts
              wp_reset_postdata();
            } // end if $posts
            
            // here is where your loop starts
            // this while should probably contain 
            // the divs tout-single-doctor & tout-more-specialists
            while (have_posts()) {
              the_post();
              
              // This is where your 2 main divs should live
              // I think
              // more the while and end while
              
              $args = 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'
                  )
                )
              );
              $doctors = get_posts($args);
              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 
                      } // end foreach $doctors
                    ?>
                  </ul>
                <?php 
              } // end if $doctors
              
              // added reset post data here
              wp_reset_postdata();
              
            } // end while have_posts()
          ?>
        </div><!-- end single-specialty-container -->
      </div><!-- end tout-more-specialists -->
      <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(); ?>
    

    The following is what I assume you are looking for, but I don’t know for sure, I am only guessing.

    
    <?php get_header(); ?>
    <div class="row" data-equalizer="bridge">
      <?php 
        // moved while outside of two main divs
        while (have_posts()) {
          the_post();
          ?>
            <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) {
                    foreach ($posts as $post) {
                      setup_postdata($post);
                      ?>
                        <a class="doctor-specialty-link" href="<?php 
                            the_permalink(); ?>"><?php the_title(); ?></a>
                      <?php 
                    } // end foreach $posts
                    wp_reset_postdata(); 
                  } // end if $posts
                  
                  the_post_thumbnail('full');
                  
                  $posts = get_field('doctor_services');
                  if ($posts) {
                    foreach ($posts as $post) {
                      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 
                    } // end foreach $posts
                    wp_reset_postdata();
                  } // end if $posts
                  
                  // cleaned this up a bit to reduce code
                  // but all of the get_field() calls
                  // need to be in The Loop
                  $fields = array(
                    'doctor_biography' => 'Provider Biography',
                    'doctor_education' => 'Provider Education',
                    'doctor_residency' => 'Provider Residency',
                    'doctor_internship' => 'Provider Internship',
                    'doctor_fellowship' => 'Provider Fellowship',
                    'doctor_board_status' => 'Board Status'
                  );
                  foreach ($fields as $field => $label) {
                    $value = get_field($field);
                    if ($value) {
                      ?>
                        <p>
                          <span class="doctor-background-title"><?php 
                              echo $label; ?>:</span>
                          <?php echo $value; ?>
                        </p>
                      <?php 
                    } // end if $value
                  } // end foreach $fields
                ?>
                
              </div><!-- end single-doctor-container -->
            </div> <!-- end tout-single-doctor -->
          
            <div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
              <div class="single-specialty-container">
                <?php 
                  
                  // I don't understand why you need this
                  // can probably be removed
                  $this_post = $post->ID;
                  
                  $posts = get_field('doctor_specialties');
                  if ($posts) {
                    foreach ($posts as $post) {
                      setup_postdata($post);
                      ?>
                        <h2 class="related-specialties-title">
                          <a class="doctor-specialty-link" href="<?php 
                              the_permalink(); ?>">More <?php 
                              the_title(); ?> Specialists</a></h2>
                      <?php 
                    } // end foreach $posts
                    wp_reset_postdata();
                  } // end if $posts
                  
                  
                  // this is where The Loop used to start
                    
                  $args = 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'
                      )
                    )
                  );
                  $doctors = get_posts($args);
                  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 
                          } // end foreach $doctors
                        ?>
                      </ul>
                    <?php 
                  } // end if $doctors
                  
                  // added reset post data here
                  wp_reset_postdata();
                    
                  // this is where The Loop Used to End
                ?>
              </div><!-- end single-specialty-container -->
            </div><!-- end tout-more-specialists -->
          <?php 
        } // end while have_posts
      ?>
      <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(); ?>
    

    If I’ve got this right it may be easier for you to figure out where you need to add the if/else code. With the changes I’ve made I can’t be sure what needs to be replaced.

  • Thank you again for taking the time to not only write me back but also clean up my code. I am still learning as I go (trying my best to improve) and appreciate being pointed in the right direction. The code you provided gets everything to display only if I remove the wp_reset_postdata(); from

      $this_post = $post->ID;
        $posts = get_field('doctor_specialties');
        if ($posts) {
          foreach ($posts as $post) {
            setup_postdata($post);
            ?>
              <h2 class="related-specialties-title">
                <a class="doctor-specialty-link" href="<?php 
                    the_permalink(); ?>">More <?php 
                    the_title(); ?> Specialists</a></h2>
            <?php 
          } // end foreach $posts
          //wp_reset_postdata();
        } // end if $posts

    If I keep the wp_reset_postdata then doctors with the same specialty do not display. I am still trying to figure out the best solution for doing the if else statement. The If else has to be outside of the div.tout-more-specialists, so whatever I am checking with the if statement must exist outside of the div itself. When I var_dump ($doctors) outside of the div.tout-more-specialists NULL is returned. This makes sense because I am not even calling the doctors array until later in the code. But if I var_dump ($doctors) inside the code after the doctors array I see the list of doctors that have the same specialties (example pediatrics has many doctors), and on the doctors single page that have no like specialties like (neurology), this is returned array(0) { } . So I know that I need to check to see if ($doctors) returns a full array, else display the other div. I am just trying to figure out how to write the if else statement outside of the div.tout-more-specialists. So far everything I have tried ends up breaking the code. Thank you again for your time John, you are extremely generous for all your help.

  • If you don’t reset the post data at that point, the next time that a post is refereed to it will be looking at the last post in the previous loop. So if we look at this section of code

    
    
                <?php 
                  
                  // I don't understand why you need this
                  // can probably be removed
                  $this_post = $post->ID;
                  
                  $posts = get_field('doctor_specialties');
                  if ($posts) {
                    foreach ($posts as $post) {
                      setup_postdata($post);
                      ?>
                        <h2 class="related-specialties-title">
                          <a class="doctor-specialty-link" href="<?php 
                              the_permalink(); ?>">More <?php 
                              the_title(); ?> Specialists</a></h2>
                      <?php 
                    } // end foreach $posts
                    // if we don't reset the post data here
                    // the last post used in the above loop
                    // will be used in the get_the_ID()
                    // function call below
                    wp_reset_postdata();
                  } // end if $posts
                  
                  
                  // this is where The Loop used to start
                    
                  $args = 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'
                      )
                    )
                  );
    

    'value' => '"' . get_the_ID() . '"', The ID returned will be for the last post in the previous loop and not for the post that’s actually being displayed. The only time it would return the current post ID would be if
    $posts = get_field('doctor_specialties'); returned false.

    Maybe what you really want to do is put the second loop inside the first one, but honestly, I can’t say for sure. There is a lot going on in this template, some of which is hard to follow without understanding how all of these things relate to each other.

  • Hey John, I ended up finding a solution. Thank you again for all your help. I really took a lot away from your suggestions and in the future will be developing and coding in a different way. THANK YOU.

    Here is what worked, I know it may not be the cleanest.

    <?php get_header(); ?>
    <div class="row" data-equalizer="bridge">
      <?php
        // moved while outside of two main divs
      while (have_posts()) {
        the_post();
        ?>
        <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) {
              foreach ($posts as $post) {
                setup_postdata($post);
                ?>
                <a class="doctor-specialty-link" href="<?php
                the_permalink(); ?>"><?php the_title(); ?></a>
                <?php
                    } // end foreach $posts
                    wp_reset_postdata();
                  } // end if $posts
    
                  the_post_thumbnail('full');
    
                  $posts = get_field('doctor_services');
                  if ($posts) {
                    foreach ($posts as $post) {
                      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
                    } // end foreach $posts
                    wp_reset_postdata();
                  } // end if $posts
    
                  // cleaned this up a bit to reduce code
                  // all of the get_field() calls
                  // need to be in The Loop
                  $fields = array(
                    'doctor_biography' => 'Provider Biography',
                    'doctor_education' => 'Provider Education',
                    'doctor_residency' => 'Provider Residency',
                    'doctor_internship' => 'Provider Internship',
                    'doctor_fellowship' => 'Provider Fellowship',
                    'doctor_board_status' => 'Board Status'
                    );
                  foreach ($fields as $field => $label) {
                    $value = get_field($field);
                    if ($value) {
                      ?>
                      <p>
                        <span class="doctor-background-title"><?php
                          echo $label; ?>:</span>
                          <?php echo $value; ?>
                        </p>
                        <?php
                    } // end if $value
                  } // end foreach $fields
                  ?>
    
                </div><!-- end single-doctor-container -->
              </div> <!-- end tout-single-doctor -->
    
              <?php
              $hasRelatedContent = false;
              $this_post = $post->ID;
              $posts = get_field('doctor_specialties');
              if ($posts) {
                foreach ($posts as $post) {
                  setup_postdata($post);
                    } // end foreach $posts
                    // wp_reset_postdata();
                  } // end if $posts
    
                  $args = 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'
                        )
                      )
                    );
                  $doctors = get_posts($args);
                  if ($doctors) {
                    $hasRelatedContent = true;
                  }
    
                  if($hasRelatedContent == true) {
                    wp_reset_postdata();
                    ?>
    
                    <div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
                      <div class="single-specialty-container">
                        <?php
    
                        $this_post = $post->ID;
                        $posts = get_field('doctor_specialties');
                        if ($posts) {
                          foreach ($posts as $post) {
                            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
                    } // end foreach $posts
                    // wp_reset_postdata();
                  } // end if $posts
    
                  $args = 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'
                        )
                      )
                    );
                  $doctors = get_posts($args);
                  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
                          } // end foreach $doctors
                          ?>
                        </ul>
                        <?php
                  } // end if $doctors
    
                  wp_reset_postdata();
                  ?>
                </div><!-- end single-specialty-container -->
              </div><!-- end tout-more-specialists -->
              <?php
            } // end if has content check
            else {
              ?>
              <div class="large-4 medium-4 small-12 columns tout-bridge-patient-portal_home">
                <!-- Patient Portal Widget -->
                <?php get_template_part( 'partials/global-custom-fields/patient', 'portal' ); ?>
              </div>
              <?php
            }
            ?>
    
            <?php
        } // end while have_posts
        ?>
        <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(); ?>
Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.