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

Okay I have a custom loop within the main loop of the page that shows 4 posts but obviously excludes the current main post. I'll show you:

<?php $my_query = new WP_Query( "cat=3&posts_per_page=4" );
    if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post();
        if($post->ID == $mainPost) continue; ?>
        <?php get_template_part('workThumb'); ?>
    <?php endwhile; endif; wp_reset_postdata(); ?>

However I want to show 4 posts in this loop but obviously if it contains the current post then it only shows 3. However if I increase the posts_per_page to 5 then on pages when it doesn't include the current post it shows 5 posts, which I don't want.

I hope this makes sense. Any ideas?

share|improve this question
up vote 1 down vote accepted

change your query to:

<?php $my_query = new WP_Query( array("cat" => 3, "posts_per_page" => 4, "post__not_in" => array($mainPost)));


share|improve this answer
Woop! Yes man you're a genius! Thank you very much. – mattl May 13 '11 at 9:56

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.