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

I list my two most recent posts from a widget on the sidebar. When I'm viewing one of these posts from single.php, I want that post to be excluded from the list and instead show the next post in order.

single.php looks something like this:

<?php if ( have_posts() ) : ?>

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


    <?php endwhile; else : ?>

    [If no post is found]

<?php endif; ?>

and here's the code in the PHP-widget:

<?php $the_query = new WP_Query( 'showposts=2' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

    [Recent posts]

<?php endwhile;?>

Edit - the solution:

<?php global $post; $args = array('showposts' => 2, 'post__not_in' => array( $post->ID )); query_posts( $args ); if (have_posts()) : while (have_posts()) : the_post(); ?>

    [Recent posts]

<?php endwhile; wp_reset_query(); endif; ?>
share|improve this question
see the first question in related posts column over there ---> – Milo Mar 19 '13 at 0:38
up vote 1 down vote accepted

The post__not_in arg should work for you:

$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => 7,
'post__not_in' => array( $post->ID )
$myposts2 = get_posts($args);
share|improve this answer
I tried it but nothing happens. Probably because I don't quite understand how, or where to place it correctly. – Lightshadow Mar 19 '13 at 16:39
I finally got it working, thanks. Edited my first post. – Lightshadow Mar 22 '13 at 15:28
glad I could help :) – Andrew Hendrie Mar 22 '13 at 23:57

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.