Over at WordPress support, it isn't all that unusual to see folks wondering how to exclude certain posts from their front page. I typically suggest they grab a "category excluder" plugin[1] and add the posts they don't want to see to a certain category.
The question came up again today, but the person was looking for something that wouldn't be category based. That got me thinking...
Well... I suppose you could look into a bit of theme hacking. Perhaps with a custom field (see Using_Custom_Fields) and then modify The_Loop to exlude posts with a custom field at a certain value?
Something to ponder at any rate.
So I pondered.
What I came up with is overly simple, but works so I figured I'd share.
First, edit the post that you don't want to show on the front page and add a custom field of "exclude" to it. The value of that field can be anything... we're not going to check the value, we just want to see if the custom field exists for the post.
Do this each time you want to set a post to not show in the main list.
You'll find the custom field section on your post editor page, just scroll down a bit past the image uploader. Click that picture to the right for an example.
Now, to actually use that custom field, we need to add two lines of code to your theme's index.php.
In most themes' index.php, you'll see a line like this:
-
<?php while (have_posts()) : the_post(); ?>
Right below that, add a new line of code. The result will look like this:
-
<?php while (have_posts()) : the_post(); ?>
-
<?php if ( get_post_meta($post->ID, 'exclude', true) == '') { ?>
That new line basically says, "look for the custom field named "exclude" associated with the post. If it is NOT there (that's the == '' bit) then proceed. As you might guess, if the custom field does exist, we pretty much drop it on the floor and move along.
A bit lower in the code, you'll see:
-
<?php endwhile; ?>
Right before that line, let's close that if statement we just added:
-
<?php } ?>
-
<?php endwhile; ?>
Done!
Caveats!
Some WordPress theme's use their index.php file for a variety of views and purposes. If your theme is one of those, this will probably wreak havoc and likely drop locusts from the sky. Let's try to avoid that.
First off, a glance at the Template Hierarchy documentation may help what follows make a bit more sense...
Remember, we don't want the post on the front page or main listing, but we do still want it to show up in the category archives (clicking that link may be helpful as well). If your theme doesn't have any category or archive specific template files, that post you just excluded will never show up anywhere! That's not desirable... but easily fixed.
So check: Does your theme have an archive.php? A category.php? A category-x.php? Then you're probably fine. If not, copy your index.php to archive.php (undo the changes we just made in the new archive.php) and you should be good to go.
Hope that makes sense and hope this might help someone out in the future.
[1] - Two that I frequently suggest are:
- The Category Visibility plugin.
- The Ultimate Category Excluder plugin
Possibly Related posts:



28 comments
Rich G.
September 8, 2007 at 9:47 pm (UTC -7)
Why?
Would this be to keep things for feed-readers only?
Chris
September 9, 2007 at 9:48 am (UTC -7)
That would be one reason, yep. I have a simple way of excluding posts from feeds (only) too that I should write up someday.
Some folks like to only include one or two categories on the main site (site news, club announcements, etc). Some folks use WP as a Content Management System and may have a lot of content as posts, but really only want it to show in category views. Etc. etc.
Mapblog
September 10, 2007 at 8:46 am (UTC -7)
I cant keep wondering why you want to exclude posts from the front page. Blogging should be open as it states what is in your mind?
Just my 2 cents and way to go on your blog!
Chris
September 10, 2007 at 9:03 am (UTC -7)
Mapblog, did you read my earlier comment right above your comment? Secondly, did you read the first paragraph of the article?
Hopefully, combining those two will help give some context to why I wrote the article. It’s not about being “non-open”. It’s about being flexible.
Also do note, this method doesn’t “stop” anything from going through the RSS feed. Can’t be much more open than that!
Patrick Burt
September 10, 2007 at 11:01 am (UTC -7)
Good post! This is especially useful if you want to create articles as google food or posts that won’t appeal to your readers.
Rich G.
September 10, 2007 at 5:08 pm (UTC -7)
Hrm…
So I could I have index1.php that was all my post about fishing, but none of my posts about papier mache’ and have index2.php show all my papier mache’ posts but none of my fishing posts and index.php show all my posts together.
That way instead of having simplebooks.com, simplerich.com, etc…. I could have one blog with all that stuff only split the URL.
No, wouldn’t work. The feed would be terrifically confusing to all 3. I thought I’d found something wonderful for a minute there.
Chris
September 10, 2007 at 7:15 pm (UTC -7)
@Patrick – google food? Now there’s a term I hadn’t met yet!
@Rich – there may be easier ways to accomplish that through the joys of categories and category templates.
Andy
September 11, 2007 at 12:34 pm (UTC -7)
Thanks! I can actually use this bit of code for something that I have been wanting to do.
Chris
September 11, 2007 at 9:42 pm (UTC -7)
Andy, glad you found some aspect of it helpful.
… oh, and I’m very intrigued by your purple potato salad!
cms
September 12, 2007 at 3:49 pm (UTC -7)
Gotta love the web. I was looking for this exact question and found my exact answer. Thanks Chris
Chris
September 13, 2007 at 7:50 am (UTC -7)
CMS – so glad it was helpful. Thanks for stopping by.
kaiser
September 23, 2007 at 5:08 am (UTC -7)
As is got the discussion on wp-forums, i want to add this one for community over here too. another sollution would be to use a category-retriction tool. so only looged in users (who got an account) would see (for e.g) where the illegal techno party would be tonight … hehe. got the point?
Jad
July 11, 2008 at 12:11 am (UTC -7)
I did everything Fine..but the Post isn’t appearing anywhere…. BUT i have a File called ARCHIVE.PHP
Chris
July 12, 2008 at 11:33 am (UTC -7)
@Jad – archive.php isn’t the same as archiveS.php…
Marjean
August 25, 2008 at 1:04 pm (UTC -7)
How is this “overly” simple? Something that is oversimplified is a thing pared so much it is no longer accurate or functional. This does appear to be “extremely” simple, however.
Chris
August 26, 2008 at 7:22 am (UTC -7)
I’m sorry it doesn’t seem extremely simple to you.
I used the term “overly simple” to mean that the following examples are functional but a clever or more knowledgeable person would probably want to flesh things out a bit.
In other words, it’s a coarse approach that doesn’t pretend to code for edge cases.
FL
September 8, 2008 at 2:03 pm (UTC -7)
This even works on the “archive.php” or even any page that has “the loop”.
Anyway: Thank you, this really helped me out!
Sarah
November 21, 2008 at 12:50 am (UTC -7)
Thanks you so much!!
Nathan
February 6, 2009 at 5:00 am (UTC -7)
Chris, nice little work around. One thing tho, and I am not expecting you to answer this… but in theory, would it be just as easy to use the custom field to display the post.
I could be in a potential situation where we will publish more content we do not want to display on the index than content that we do.
So to keep it easier for other admins they could publish as normal but when they want something on the home they have to go the extra step. Does that make any sense?
Thanks for taking the time to post the solution above.
Chris
February 6, 2009 at 1:45 pm (UTC -7)
@Nathan – sure, that could work. Maybe just change the == to a != in that first statement and have at it
Gaurav
February 11, 2009 at 10:12 pm (UTC -7)
Wonderful tip, made my work very simple. Thanks a ton.
Zantos
April 3, 2009 at 6:16 pm (UTC -7)
That was really helpfull thank you so much
Maddy
April 13, 2009 at 9:35 am (UTC -7)
good one dude ..thumbs up
bobbyt
June 1, 2009 at 6:34 pm (UTC -7)
Hey Chris,
Thanks for the everything, except I wanted to be able to see galleries whenever I repost the excluded post on a different page. Any idea how I can do this?
AcornRev
December 19, 2009 at 8:03 pm (UTC -7)
Thanks! This is perfect and simple!
Anthony
February 18, 2010 at 10:17 am (UTC -7)
Thank you. Thank you. Thank you. The second line of your post that says: “category excluder” plugin was the solution for me. I use the mCatFilter Option (awesome). And I setup a catagory “No Display on Front Page” linked my post to it, and poof…it was gone from the front page. Thanks again..
David
November 29, 2010 at 5:29 am (UTC -7)
An awesome solution – thanks!
Ali
June 1, 2011 at 11:36 am (UTC -7)
I also tried to use your code snippet in the Twentyten theme to check whether it would work. Unfortunately, it doesn’t. I made everything you have written in the blog post except of the fact that I put the snippet in loop.php:
ID, 'autor1', true) == '') { ?>
Unfortunately, I get that error message: “Parse error: syntax error, unexpected T_ENDWHILE in /var/www/web948/html/testumgebung/wp-content/themes/twentyten/loop.php on line 182″.
If I erase the endwhile part, the frontend shows all articles.
Any suggestion what is to do?