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 have found a lot of documentation on solving this issue with multiple loops, I'm finding it hard to see a solution for one custom loop.

Problem: I'm using infinite scroll to load in the next page of posts to the homepage. The posts need to be shown in a random order so I need to store which posts have been shown and then exclude them the second time the query is hit.

<?php $my_query = new WP_Query(array('post__not_in'=> $do_not_duplicate, 
                        'orderby' => 'rand',
                        'category_name' => 'projects', 
                        'posts_per_page' => 20, 
                        'paged' => $paged));
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>

<?php get_template_part( 'entry' ); ?>
<?php endwhile; ?>

I have built this from http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action - however the approach there is to use $do_not_duplicate in the second loop.

Perhaps I need to shuffle this around a bit, store the posts shown in another way, a cookie? Any guidance appreciated.

share|improve this question
I don't (completely) get what you're doing right now. Why are you setting $do_not_duplicate to the current post's ID - for each post? Don't you want to keep track of all the posts already handled? I'd rather store the post IDs in an array (and then eventually implode it to get a comma separated string), or create that string by hand (meaning concatenating). –  t f Aug 3 '13 at 14:41
Infinite scroll is gong to make a new request to the server for the next set of posts. That is the nature of AJAX. That means that your $do_not_duplicate variable will never be populated when it needs to be. You need to find a way to pass that information to the infinite scroll script. I don't see the WordPress specific part of this though. You have that part right, though using paged makes no sense in this context. –  s_ha_dum Aug 3 '13 at 15:07
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer


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

Browse other questions tagged or ask your own question.