Support

Account

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

  • 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.