Exclude Single Post in WordPress

If you would like to exclude one post from your blog, archive, search results or wherever you need to on your blog, you can do so by putting in a small piece of code within the particular WordPress loop:

The following example will show all posts except for the post with the ID of 179:

PHP:
  1. <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == '179' ) continue; ?>
  2. <?php the_content();?>
  3. <?php endwhile;endif;?>

This is something that I get a lot of requests for and is very useful in a number of situations.

33 Responses to “Exclude Single Post in WordPress”

  1. Rene says:

    Thanks “Ran Yaniv Hartstein”.
    It´s works!!!!

  2. I added a bit to make this dynamic to work with my work-single pages. Now the code excludes the current page and loops through all other projects. Smart…

    ID == ”.$current_post.” ) continue; ?>

  3. Kai says:

    This works beautifully, especially with Bjarne’s dynamic addition.

    My only question is: WHY does this work? I’m relatively new to Wordpress development and the way this read seems to defy logic. I’m reading it is “If there is a post, and while there is a post, then return the post. If $post->ID is equal to number 179, then continue.” And I come up with two possible outcomes with that logic:
    1) The loop will continue as long as $post->ID == 179 is true, in which case we should see ONLY this post.
    2) The loop will continue because at one point $post->ID does equal 179 and therefore will continue, in which case we will see ALL posts, including this one.

    How we get to showing all posts EXCEPT this one is hard for me to see. Is it that the loop “skips” when it reads $post->179 as true, thus giving us the full loop minus the one hiccup??

    Any insight you can give me will be greatly beneficial! Thanks! And thanks for the solution! Some of the crazy contraptions that people come up with for this solution are complex and glitchy and take up valuable space (and time!). This answer has significantly improved my current project.

  4. @Kai ‘continue’ skips the current loop

    I hope that helps.

  5. cj says:

    what about if you have more than 1 post to exclude what would be the code?

    this one?

    1.
    ID == ‘179,180,181′ ) continue; ?> ?

    need your help

  6. Kai says:

    No, I believe that will always be false, and you will not get what you’re looking for.

    Try this using your cat IDs (tested in WP 2.9):

    if (ID == ‘179′ || ID == ‘180′ || ID == ‘181′) continue;

    There may be a more streamlined way, but the ID will never be equal to an array, so ID == ‘179,180,181′ will always be false.

    Hope it helps!

  7. cj says:

    hi kai, thanks for your response.

    I tried having this code

    ID == ‘179? || ID == ‘328? || ID == ‘325?) continue; ?>

    however my posts are still showing in the homepage. those are my 3 posts that i don’t wish to be seen im my home page. how will i achieve this ?

    • Kai says:

      Can you post the full query you’re trying to run? There may be something else at play here.

      In the meantime, replace those “?” with single a single quote ‘. Also, sometimes when code snippets get posted, they auto format into curly/angled quotes. Replace these curly ones in your code with a single quote that appears vertical. I retested in WP 2.9 and it’s working 100%:

      if (ID == '179' || ID == '180' || ID == '181') continue;

    • cj says:

      thanks,

      I believe this is the code that posts my posts in the blog

      i tried your code again but no luck…hope you can help me with this…thanks so much

  8. cj says:

    sorry my codes is not showing. i dunno what tag to be used to show the php code anyways here’s the code after the php tag

    if (have_posts()) : while (have_posts()) : the_post();

Leave a Reply