WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Within my loop I want to display 9 posts that exclude categories X, Y, Z and all sticky posts.

Between post 3 and 5 I would like to place a sticky post with a custom template.

The below works except that it treats the sticky post as the 4 post from the first loop which results in one regular post missing.

I have to use WP 3.0.3

<?php if (have_posts()) : ?>
    <?php $count = 0;?>
      $sticky_posts = get_option('sticky_posts');
      $arguments = array(
          'posts_per_page' => 9,
          'post__not_in'  => $sticky_posts,
          'category__in'  => array(1,2,3,4,5)
    <?php query_posts( $arguments );?>

    <?php while (have_posts()) : the_post(); ?>

        <?php $count++; ?>

        <?php if ($count == 4) : ?>

              $sticky = get_option('sticky_posts');
              $args = array(
                  'posts_per_page' => 1,
                  'post__in'  => $sticky
              <?php query_posts( $args );?>

              <?php while (have_posts()) : the_post(); ?>

              <h1>Latest Sticky Post</h1>
              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
              <?php the_excerpt(); ?>

              <?php endwhile; ?>
              <?php wp_reset_postdata(); ?>

        <?php else : ?>

            <h1>Regular Post</h1>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php the_excerpt(); ?>

        <?php endif; ?>
    <?php endwhile;  ?>
<?php endif; ?>
share|improve this question
You should (really) not wrap a loop inside a loop. Better is to save your query results into different vars and loop them affterwards. – kaiser May 10 '11 at 16:24

you don't need if/else on the counter, just check if $count == 3 or 5 to inject a sticky post, insert a regular post in every iteration of the loop, and increment the counter at the bottom.

share|improve this answer

Your Answer


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.