Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm experiencing an odd issue using Infinite Scroll for the WP_Query loop on my homepage. Half the time when the second page fires, I'm getting a few duplicate posts showing up in the second set. I can't find a pattern for why this is happening so I'm looking for a better way to tell the infinite scroll script which posts to get. Here's what we're looking at.

   <?php
     global $wp_query;
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
     $args = array(
        'meta_key' => 'hotness',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'cat' => -1,
        'posts_per_page' => 40,
        'post_type' => array( 'post','fod_videos','fod_music','fod_articles', 'fod_albums'  ),
        'paged' => $paged
     );
    $wp_query = new WP_Query( $args );
    while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
     <?php get_template_part( 'loop', 'contents' ); ?>
    <?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
 <div class="infinitescroll">
   <?php next_posts_link( __( 'Load more posts' ) ); ?>
 </div>
<script type="text/javascript">
var href = 'first';
$(document).ready(function() {
    $('#boxes').infinitescroll({
        loading: {
          finished: undefined,
          finishedMsg: '',
          img: '',
          msgText: '',
          speed: 'fast',
          start: undefined
        },
        navSelector: '.infinitescroll',
        nextSelector: '.infinitescroll a',
        itemSelector: '#boxes .box',
        debug: false
    }, function(arrayOfNewElems) {
       var $newElems = $( arrayOfNewElems ).css({opacity: 0});
       $newElems.imagesLoaded(function(){
        $newElems.animate({opacity: 1});
        $('#boxes').masonry('appended', $newElems, true);
        if(href != $('.infinitescroll a').attr('href')) {
            href = $('.infinitescroll a').attr('href');
        }
      });
    });
});
</script>
<?php endif; ?>

Any ideas what the culprit is here or a better way to do this?

UPDATE: Looking at the source and clicking on the link to page 2 shows the duplicates in it really isn't anything to do with the script as it my pagination is off for some reason.

It seems like removing

        'meta_key' => 'hotness',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',

it paginates correctly("seems"). So I'm thinking maybe sorting by the meta key isn't being recognized by pagination?

The problem seems to be when the pages break at posts that have the 'hotness' value of 0. Not sure why.

share|improve this question
1  
what is this hotness value? –  Tom J Nowell Oct 1 '12 at 16:02
    
hotness is the days ago of the post divided by the ratings score. Just stored as a number. –  Pollux Khafra Oct 1 '12 at 16:06
1  
Is it possible that this value changes between the initial load and the infinite scroll loading the next page? –  Tom J Nowell Oct 1 '12 at 16:10

1 Answer 1

I have never seen a custom loop like that, you should try getting rid of

<?php get_template_part( 'loop', 'contents' ); ?>

and replace it with

 the_content();
share|improve this answer
1  
It's that way for a reason. My homepage is showing 40 posts to start so obviously Im not displaying the content of the posts. It's just a file that contains the content for each box in the loop which is complex. –  Pollux Khafra Oct 1 '12 at 3:32
1  
This is a standard practice in WordPress to use get_template_part() –  Brian Fegter Oct 1 '12 at 3:35
    
I understand that. But when you put get_template_part() in your theme, isn't that file suppose to conatain the loop or query? What would happen if he has his query above and then another query in the template part? wouldn't that cause double posts? –  Jamie Oct 1 '12 at 12:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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