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.

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.

<?php
$currentID = get_the_ID();
$my_query = new WP_Query( array('cat' => '1', 'showposts' => '5', 'post__not_in' => array($currentID)));
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h2>
<?php the_content(); ?>
<?php endwhile; ?>

21 replies on “Exclude current post from WP Query

  1. Avatar for rajeev rajeev says:

    thank you

  2. Avatar for Keyur Amin Keyur Amin says:

    Thanks mate

  3. Avatar for randzgonz02 randzgonz02 says:

    Perfect for my need. Thanks! πŸ™‚

  4. Avatar for Rajesh Raj Rajesh Raj says:

    Very Simple Thank You….

  5. Avatar for Rodrigo Zuluaga Rodrigo Zuluaga says:

    Thanks!!

  6. Avatar for Debendra Maharjan Debendra Maharjan says:

    To the point, thank you

  7. Avatar for 醉拳 醉拳 says:

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

  8. Avatar for david david says:

    this helped me out πŸ™‚
    thanks!

  9. Avatar for chris chris says:

    Excellent !
    Could you help make the title clickable ?
    Thanks,
    Chris

    1. Avatar for daretothink daretothink says:

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

  10. Avatar for Francesco Cortese Francesco Cortese says:

    Great, thanks!

  11. Avatar for WoutervanderZee WoutervanderZee says:

    Thanks Alan, just what I was looking for!

  12. Avatar for sdfasd sdfasd says:

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

  13. Avatar for Jacob Stoops Jacob Stoops says:

    Thanks for this. Worked like a charm!

  14. Avatar for hosseincode hosseincode says:

    tnq mod

  15. Avatar for Aaron Weber Aaron Weber says:

    Perfect, thanks so much.

  16. Avatar for Anonymous Anonymous says:

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

    1. Avatar for Anonymous Anonymous says:

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

      1. Avatar for chris chris says:

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

  17. Avatar for Thomas Wiese Thomas Wiese says:

    thnx this has has helped me alot πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *