delarge
5/3/2018 - 11:22 PM

WP related panels

From the ACF relationship field - with check in query contains correct results

<?php

// get only first 3 results
$ids = get_field('related__content', false, false);


// args
$args = array(
  'post_type'      	=> 'any',
  'posts_per_page'	=> 3,
  'post__in'			=> $ids,
  'post_status'		=> 'publish',
  'orderby'        	=> 'post__in',
);


$the_query = new WP_Query( $args ); ?>

<?php

if (!empty($the_query->query_vars['post__in'])) { // if the post_in query var is not empty...
  
  
  ?>
  
  <section class="related-panels">
    
    <div class="holder">
      <?php if (get_field('related__title')): ?>
        <h2><?php the_field('related__title');?></h2>
      <?php endif; ?>
    </div>
    
    <div class="holder">
      
      <?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
        
        
        <div class="related-panels__panel">
          <a href="<?php the_permalink(); ?>">
            
            <?php
            the_post_thumbnail( '372x245' );
            ?>
            
            <div class="panel-text">
              <h2><?php the_title(); ?></h2>
            </div>
          </a>
        </div>
        <!-- related-panels__panel -->
        
        
      <?php endwhile; endif; wp_reset_postdata(); ?>
      
      
    </div>
    <!-- holder -->
  </section>
  
<?php } // if not empty ?>