I am having trouble with multiple loops and sticky posts in wordpress.

The first loop I just want all the sticky posts.

<?php query_posts(array('post__in'=>get_option('sticky_posts'), 'cat' => '-15,-17, -5, -2')) ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>   

<?php $do_not_duplicate = $post->ID; ?>

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

<?php endwhile; endif; ?>

In the second loop I want to get the all of the posts that are NOT sticky (so the sticky posts don't show). Currently all but the latest sticky post still show in the loop below.

Also the posts_per_page is including the posts in the first loop which I do not want. No matter how many sticky posts I add I want the number of posts that are NOT sticky to be consistent.

<?php query_posts($query_string . '&cat=-15,-17, -5, -2&post_type=post&posts_per_page=9'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
<?php the_first_category(); ?>
<a" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>

<?php endwhile; ?>

<?php next_posts_link('Older &rarr;') ?>

<?php endif; ?>

Can anyone help or point me in the right direction?

  • Use ignore_stickies=1 in the second loop, or if using wp 3.0, then use caller_get_posts=1 instead(same thing, just different name in 3.1).
    – t31os
    Mar 7, 2011 at 15:20

2 Answers 2


your problem is that in the first loop you overwrite $do_not_duplicate for each post in the loop so at the end you only have the id of the last post. if you change $do_not_duplicate into an array then you can add each post to the array:

//before the loop
$do_not_duplicate = array();

//in the loop

$do_not_duplicate[] = $post->ID;

then you can just use in in the second query :

<?php query_posts($query_string . '&cat=-15,-17, -5, -2&post_type=post&posts_per_page=9&post__not_in='.$do_not_duplicate); ?>
  • Thanks for the code but more importantly the explanation.
    – Craig
    Mar 8, 2011 at 12:41
  • I guess its better to explain for future guest who have the same issue. Glad you got.
    – Bainternet
    Mar 8, 2011 at 13:11

use post__Not_in:

array( 'post__not_in' => get_option( 'sticky_posts' ) )

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.