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.
