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.

21 Responses to Exclude current post from WP Query

  1. rajeev says:

    thank you

  2. Keyur Amin says:

    Thanks mate

  3. randzgonz02 says:

    Perfect for my need. Thanks! 🙂

  4. Rajesh Raj says:

    Very Simple Thank You….

  5. Rodrigo Zuluaga says:


  6. Debendra Maharjan says:

    To the point, thank you

  7. 醉拳 says:

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

  8. david says:

    this helped me out 🙂

Leave a Reply

Your email address will not be published.