Hide posts from WP home page

Hiding posts from showing up on the home page is something I have been thinking about all week. Last night I finally dug into the WP Codex and figured out how to make it happen. Hide WordPress posts from appearing on your home page by adding their post IDs to the following array (where you see 603 and 621), and place this code before the loop starts in your theme’s main index file.


<?php
if( is_home()){ query_posts(array('post__not_in' => array(603,621))); }
?>

This code prevents posts from showing up on your home page, but allows them to be directly accessed and shown in views other than the home page like category archive. The posts will also be included in your RSS feed.

F.A.Q.

  • Where do I put this code?

    Place the code before the loop starts in your theme’s index. To edit the index file, browse to Design > Theme Editor > Main Index Template. Place the code anywhere before this line: <?php if (have_posts()) ?>

  • How do I find the IDs of my posts?

    Go to Manage > Posts and mouseover the titles of your posts. Look at the URL showing up in your browser’s status bar. The post ID will be at the end of the edit post link URL you see.

Most ‘hidden post’ tutorials explain to use a hidden category. The problem with the category approach is the need to hide the category from showing up in the sidebar and any other place it may appear. WP does not have built in hidden or secret categories. The idea of chasing out a category everywhere categories are shown on my WP is not an attractive idea.

This code lets me publish posts that are not intended for prime time viewing but work great as a post rather than a page.

20 Comments so far

  1. Jeff Kershner on February 1st, 2009

    Corey, this is what I requested when I had DR revamped. All industry announcements are in the Industry Announcement category but do not display on the home page. I use this category for Press Releases only. I wanted to have include these press releases on DR for several reasons (that seem to be paying off).

    However, I did keep the Industry Announcements in the main RSS feed and I’m thinking about changing this and having them in a separate feed. This industry doesn’t have too many people subscribing to my RSS feed (a little over 600 maybe) but I have had a few comments mentioned that “DR had turned into a PR source”, because they only read the RSS feeds and not visit the site.

    Any suggestions on breaking up the RSS feed?

  2. Corey on February 3rd, 2009

    Jeff, category level feeds are built in. Here is that category’s feed:

    http://www.dealerrefresh.com/category/automotive-industry-announcements/feed/

    There are a few ways to exclude categories from an RSS feed. I will give it some thought.

  3. esanstudio.net on March 8th, 2009

    i tr this technique but it is does not work.
    but i try this. http://codex.wordpress.org/Template_Tags/query_posts

    it is worked!! how ever of that you give me the idea.thanks

  4. Corey on March 10th, 2009

    esan, I am using this code on almost all of my WP sites now, so I am confident that it does work. I am glad you found a solution to your particular problem.

  5. Piesto on March 25th, 2009

    Thank you so much. I was searching for it since 2 hours!
    Usefull weblog :D

  6. Mohit on August 2nd, 2009

    works perfectly. I think the guy above who says it doesn’t work was just trying to promote his own site.

  7. Jim on August 28th, 2009

    I want to show only my latest post on my homepage, but for some reason, it shows all of my posts.

    When I click on the post’s ‘links’ in my menu, each post shows individually, which is the way they should.

    I’m using WP and have set my posts number to “1″ in my Reading settings, but it has no effect.

  8. Corey on August 28th, 2009

    Jim:

    If you change the setting and it has no effect on your site, you may be using a theme that has hard-coded changes that bypass the settings. If you change your site to a different theme temporarily, does it work?

  9. Jim on August 28th, 2009

    No it doesn’t. I’ve activated the WP Default theme and a couple of others and the homepage still shows all the posts.

  10. Artur on September 26th, 2009

    Many thanks Corey, it works :)

    the only question I have:
    how do you specify which post you’d like to show up on Home Page?

    I’ve changed array(603,621) to array(1) and it shows my latest post, my aim is to make it show only my first post on home page

    Thanks a lot for your help

    Regards,
    Artur

  11. Corey on September 27th, 2009

    Artur:

     

    Using array( 1 ) simply excludes your oldest post from showing on the home page. Your latest post would have the highest number. This code example demonstrates how to hide posts from the home page. To change the number of posts that are shown on the home page, there is an option in Settings >> Reading to change the number of posts per page. You could change this setting to 1 to accomplish what I perceive to be your goal.

  12. Artur on September 28th, 2009

    Brilliant!

    Thanks a lot Corey.

  13. Michael on October 28th, 2009

    Tnx. I was trying to hide posts, but no luck till found your manual.

  14. Bert on February 11th, 2010

    Hello,

    This is exactly what I need for a project I am working on, but can’t seem to get it to work.

    I think it is with the array #’s, but don’t know for sure.

    Can you please advise?
    -
    Thank you,
    Regards,
    Bert

  15. 9swords on May 14th, 2010

    Thank you for sharing this, i found this really handy for tucking away a holiday post that was looking out of place.

  16. Maulik on August 26th, 2010

    Thanks for this update. Found it really helpful.

  17. Max SG Whois on September 1st, 2010

    why not use exclude – &exclude=POST-ID

  18. Corey on September 22nd, 2010

    In what context?

  19. Anson on March 2nd, 2011

    Corey, great post, it was exactly what I was looking for. It excludes the posts great.

    I have found one bug and I’m not sure if I’m the only one. After inserting the code if you click on “older posts” at the very bottom of the index page, instead of showing older posts it just shows my index page again but the url says that it is on page two.

    Let me know if you have any suggestions, other than that it worked well.

  20. Corey on March 2nd, 2011

    Anson:

    You are right. I don’t know why this behavior occurs.

Leave a reply

 

Thanks for reading!

Sign up for email updates: