0

I have a single.php with 3 loops one the page (related posts, latest posts and the actual content).

All loops are using the wp_reset_postdata(); at the end.

For some reason my post_per_page argument for related and latest posts are retuning an extra post.

I'm not sure what is wrong. Any ideas?

Thanks for the help.

<?php get_header(); ?>

<!------------------------------------------------ 
--------------------------------------------------
Hero Section
--------------------------------------------------
-------------------------------------------------->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

  <div id="hero" style="background-image: url('<?php bloginfo( 'template_directory' ) ;?>/images/slider_blog.png')">
    <div class="row">
      <div class="small-12 columns text-center">   
        <div class="slider_text_box white">
          <h1><?php the_title(); ?></h1> 
        </div>     
      </div>
    </div>
  </div>

<!------------------------------------------------ 
--------------------------------------------------
Content Section
--------------------------------------------------
-------------------------------------------------->
  <?php $time = get_post_meta( get_the_ID(), 'single_time', true ); ?>

  <div class="row small_section">
    <div class="small-12 medium-8 columns content">
      <div class="meta_blog text-center">
        <?php the_date(); ?> | Read Time: <?php echo $time; ?> min  | <?php the_category( ' ' ); ?> 
      </div>
      <div>
        <?php the_content(); ?>
        <div class="meta_blog text-center">
          <?php echo get_the_tag_list( 'Tags: ', ', '); ?>
        </div>
      </div>
  <?php endwhile; 
  wp_reset_postdata();
  else : ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>
    </div>
      <aside class="show-for-medium medium-4 large-3 large-offset-1 columns">
       <div class="row text-center" id="sidebar">
       <!-- Search -->
         <div class="small-12 columns">      
           <form>
             <input type="text" placeholder="Search"></input>
           </form>      
         </div>
         <!-- Newsletter signup -->
         <div class="small-12 columns">
           <div class="sidebar_containers">
             <h5>Newsletter</h5>
             <p class="small_bottom_room">Stay in Touch</p>
             <form>
               <input type="text"></input>
               <button class="small_button_orange">Sign Up</button>
             </form>
           </div>      
         </div>
         <!-- Recents Posts -->
         <div class="small-12 columns">
           <div class="sidebar_containers">
             <h5 class="small_bottom_room">Latest Posts</h5>

             <?php $args2 = array(
               'posts_per_page' => 1,
             ) ?>

             <?php $latest_posts = new WP_Query( $args2 ); ?>

             <?php if ( $latest_posts->have_posts() ) : while ( $latest_posts->have_posts() ) : $latest_posts->the_post(); ?>
               <div class="latest_posts">
                 <a href="<?php the_permalink(); ?>">
                   <?php the_title(); ?>
                 </a>
               </div>          

             <?php endwhile; 
             wp_reset_postdata();
             else : ?>
             <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
             <?php endif; ?>
           </div>      
         </div>

         <!-- Follow Us -->    
         <div class="small-12 columns">
           <div class="sidebar_containers">
             <h5 class="small_bottom_room">Follow Us</h5>
             <a href="#"><img src="<?php bloginfo( 'template_directory' );?>/images/icon_facebook_orange.png" alt="" width="45" height="45"></a>
             <a href="#"><img src="<?php bloginfo( 'template_directory' );?>/images/icon_instagram_orange.png" alt="" width="45" height="45"></a>
             <a href="#"><img src="<?php bloginfo( 'template_directory' );?>/images/icon_linkedin_orange.png" alt="" width="45" height="45"></a>
           </div>      
         </div>
       </div>
     </aside>
  </div>



<!------------------------------------------------ 
--------------------------------------------------
Related Posts Section
--------------------------------------------------
-------------------------------------------------->

<section class="blue_bg">
 <div class="row" data-equalizer>
  <h2 class="text-center smallest_section">You Might Also Like This</h2>

<?php $args3 = array(
  'posts_per_page' => 5,
  'post_type' => 'post'
 ); ?>

<?php $related_posts = new WP_Query( $args3 ); ?>

<?php if ( $related_posts->have_posts() ) : while ( $related_posts->have_posts() ) : $related_posts->the_post(); ?>

  <?php get_template_part( 'template_parts/related-posts' ); ?>

<?php endwhile; 
wp_reset_postdata();
else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

  </div>
</section>

<?php get_footer(); ?>
2
  • Your code does not have anything to correct except wp_reset_postdata() after first loop. Main query does not required it. You might want to check some filters in plugins, themes. Try disabling all plugins and switching to different theme.
    – Sumit
    Commented Apr 1, 2016 at 17:18
  • Thanks for taking a look. Got rid of the wp_reset_postdata() for the first loop and still having the same issue. Also don't have any plugins. Any other ideas?
    – Peekay
    Commented Apr 1, 2016 at 17:58

1 Answer 1

0

Found the issue. It had to do with one of my post being sticky. Found the answer here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.