Exclude current post from WP Query

There are times when you don’t want the current post/page you are viewing to show on the page i.e. list of recent posts, more posts from the same category and so on. Here’s how to remove that post from your query.

In my situation I had a list of posts from a certain category that I wanted to include in the sidebar (I used the php code widget to add php to my sidebar widget) but I didn’t want the post I was currently on to be displayed in that list. So here’s the code I used:

So, in the code below “get_the_ID” is fetching the ID of the current post, “cat” is indicating the category I want to show posts from, “showposts” is indicating how many posts I would like to display and then finally “post__not_in” is getting the current post ID and then excluding it from the query.

  • thnx this has has helped me alot 🙂

  • Anonymous

    Thanks for the code – perfect. Just watch for the — on line 3. Needed to be => for me. 

    • Anonymous

      Thanks for the heads-up. I’ve now amended the code so all should be working well.

      • chris

        Thanks again, simple and beautiful.
        How to sort by date published (this guy is always asking for more)

  • Perfect, thanks so much.

  • hosseincode

    tnq mod

  • Thanks for this. Worked like a charm!

  • sdfasd

    7 ‘1’, ‘showposts’ => ‘5’, ‘post__not_in’ => array($currentID)));
    while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

  • WoutervanderZee

    Thanks Alan, just what I was looking for!

  • Thanks

  • Francesco Cortese

    Great, thanks!

  • chris

    Excellent !
    Could you help make the title clickable ?

    • daretothink

      Hi. I’ve just included this on the example above. It’s the_permalink

  • david

    this helped me out 🙂

  • 醉拳

    Thanks for this post. It’s still very useful.

  • Debendra Maharjan

    To the point, thank you

  • Rodrigo Zuluaga


  • Rajesh Raj

    Very Simple Thank You….