Skip to content

Instantly share code, notes, and snippets.

@clarklab
Created July 18, 2012 02:24
Show Gist options
  • Select an option

  • Save clarklab/3133709 to your computer and use it in GitHub Desktop.

Select an option

Save clarklab/3133709 to your computer and use it in GitHub Desktop.
A simple way of making sure not to display the same posts twice when using multiple loops. Save the post ID's into an array, then exclude those posts from future loops.
<?php
//start a new WP_Query with whatever parameters you want
$firstquery = new WP_Query(array(
'posts_per_page' => 2
'tag' => 'summer'
));
//start some looping action on $firstquery
while ($firstquery->have_posts()) : $firstquery->the_post();
//add the post ID to the array $excludeme
$excludeme[] = $post->ID;
//do something, like include a post template
get_template_part('loop','feature');
//end the loop
endwhile;
//start a second WP_Query that exludes the post(s) we just looped
//note the double underscore in "post__not_in"
$secondquery = new WP_Query(array(
'posts_per_page' => 5
'tag' => 'summer',
'post__not_in' => $excludeme
));
//start some looping action on $secondquery
while ($secondquery->have_posts()) : $secondquery->the_post();
//do something, like include a post template
get_template_part('loop');
//end the loop
endwhile; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment