Exclude Single Post in WordPress

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 if ( $post->ID == '179' ) continue;?>

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

51 thoughts on “Exclude Single Post in WordPress”
  1. DamionKutaeff |

    Hello everybody, my name is Damion, and I’m glad to join your conmunity,
    and wish to assit as far as possible.

  2. Tiff |

    I think this is exactly what I need, but I don’t appear to be adding it to the Loop correctly. I have a post that I need to exclude from pretty much Everywhere so that the only way to access it is from a link to that post in the menu (I need the clients to be able to leave comments, otherwise I’d just have created a page). I’ve already excluded it from the main page, archives, etc with a category exclusion, but the nav links for the other posts still know this particular post exists and I’d like them to ignore it. Help, please???

  3. jenz |

    If you want – send over the code with the loop in there and I will make sure you are adding it to the right spot (send it via the contact form)

  4. Tiff |

    I’m a moron and finally realized I can in fact turn on the comments on pages, with a little minor code tweaking…thanks for trying to help, I’ve got the more straightforward solution figured out tho! 🙂

  5. Ran Yaniv Hartstein |

    This can have the side effect of showing one post less than you wanted.

    If you want to show a certain exact amount of posts excluding a certain post (for e.g., show three latest posts from the current post’s category on every post page), use this instead (hopefuly the code will work):

    query_posts(array(
    ‘post__not_in’ => ($post->ID),
    ));

    Or, for the above example, this should work:

    $cat = get_the_category();
    $cat = $cat[0]->term_ID;
    $current_post = array($post->ID);

    query_posts(array(
    ‘more’ => 0,
    ‘showposts’ => 3,
    ‘cat’ => $cat,
    ‘post__not_in’ => $current_post,
    ));

    More info here

    http://codex.wordpress.org/Template_Tags/query_posts#Post_.26_Page_Parameters

  6. Nahum |

    Thank you for your message. Friends sent a link to it. Interestingly turned and looked, and others. Subscribe. I would have navedyvatsya

  7. Om Gargatte |

    Please help me with where exactly to place the code. The following is the index.php page of my theme.

    ———————————————————

    <div class=”post” id=”post-“>
    <a href=”” rel=”bookmark” title=”Permanent Link to “>
    <!– by –>

    Posted in |

    Not Found
    Sorry, but you are looking for something that isn’t here.

    ———————————————————

    Thank you
    Om

  8. Om Gargatte |

    <div class=”post” id=”post-“>
    <a href=”” rel=”bookmark” title=”Permanent Link to “>
    <!– by –>

    Posted in |

    Not Found
    Sorry, but you are looking for something that isn’t here.

  9. Om Gargatte |

    Hey, I got it solved….. Thank you….. is there any way so that i can avoid the post being counted totaly. As in i do not wish it to be displayed even in the recent post, categories and archieves !

  10. Luis |

    Thanks for the tip.
    But id didn’t work here, so i had to put the ‘continue’ elsewhere:

    have_posts()) : $my_query->the_post(); if( $post->ID == ‘160’ ) continue; ? >

    Now it is working fine!
    Regards.

  11. mostwanted |

    how if I would like to exclude more than one special post out of the loop?

    many thx

  12. paul |

    thank you so much, i have been searching around for 40 minutes trying to exclude a post from query_post, i want post_ID=- in wp 2.8, surely thats just logical.

    thanks alot.

  13. romonoeroetoko |

    Hm that sounds good but I would like to know more details.

  14. John Groves |

    Hey – great! This is just what I needed. Works a treat.
    Many thanks for sharing.

  15. queroeropoo |

    Good information to me.

  16. adamoerikom |

    Stunning blog and good article. High 5 for u man !

  17. WordpressThemes |

    Very nice peace of code.

  18. Schalk |

    Hi
    I created a second “blog page” by duplicating my theme’s index.php and called it mill.php. I then directed certain categories to be posted to my mill.php

    I managed to exlude the mill category from my main blog page (index.php)

    I wanted to add a sticky post to my mill.php, but even though the sticky post is only in the mill category, it shows up on the main page too?

    Any help how to prevent it? I managed to exlcude the stucky post with your code given, but it canceled all my css.

    Alternatively, How can I ad my sticky post (text and image) in the mill.php code? I will need the text to be certain colors and size too.

    thank you

  19. Schalk |

    This is my index.php

  20. Rene |

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

  21. Bjarne // Mums |

    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; ?>

  22. Kai |

    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.

  23. Freehill Media |

    @Kai ‘continue’ skips the current loop

    I hope that helps.

  24. cj |

    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

  25. Kai |

    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!

  26. cj |

    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 |

      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 |

      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

  27. cj |

    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();

  28. Veronica |

    I think the issue is because you have ID instead of $post->ID

    Try

    if ($post->ID == '179' || $post->ID == '180' || $post->ID == '181') continue;
    • Veronica |

      Whoops, that looks funny.

      Try:

      if ($post->ID == ‘179’ || $post->ID == ‘180’ || $post->ID == ‘181’) continue;

  29. Mike Goitein |

    SDAC Team – Thanks for the great post.
    There are a number of ways to handle excluding a single post from a category page, but this was the cleanest solution that I saw online.

    Well done! Hope others in the same spot have the sense to use this method first and foremost – some of the other solutions I came across were downright gnarly!

  30. Mike Goitein |

    Veronica’s last reply came in handy when I needed to exclude more than one post from a category – Kudos!!!!!

  31. Monk |

    works well, keep on contributing for the nice world… thanks all

  32. Monica |

    i have a situation where i’d like to keep the most current category posts off my category/archive page…is there any way to tweak this code so that the category page would only show the older entries? (the most current posts would stay on the index page)

    many thanks.

  33. Rai Adi Sanjaya |

    nice tricky.. 🙂
    but, now you do the same thing by query_post. like this one:
    —-

    $args = array(‘post__not_in’ => array(179));
    query_posts($args);

    —-

    Keep sharing 🙂

    • kamal |

      Thanks, this working fine. But I have a problem still now. I have total 10 post. I have excluced 3 post using this code. I wanted latest 6 post. Now it showing all rest 7 post. I want latest 6 post. can you help me anyone How can i do that?

      Here is my code:
      —————————–

      $limit,
      );
      $args = array(‘post__not_in’ => array(69,66,62));

      query_posts($args);

      ?>

      ID, ‘thumbnail_image’, $single = true); ?>
      ….
      ….
      ….

      ———————————
      please help.

  34. ????? |

    nice and efficient method. Working, strongly recommend!

  35. study abroad in US/AUSTRALIA/UK/Canada |

    learned something. the “continue” is important

  36. Abhishek Dwivedi |

    Perfect!!! This is exactly what I was looking for. Thanks a lot!

    -Abhi

  37. Jens Törnell |

    I think the Rai Adi Sanjaya method is better because that is not a trick. It’s the “real” way to do it.

  38. Beno |

    Here’s my method of doing it. You exclude the post based a variable set in a previous query when pulling a featured article. Create a “featured” blog in WordPress (with no duplicates)

  39. Lucenahin |

    i used the method its not working, im using the updated version of wordpress 3.4.2 

  40. Onlinety |

    Awesome This is great wonderful post.. Great information Thanks..

    CMS Development Services India

Leave a Reply

Your email address will not be published.
*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.