How-to: Another Way to Exclude Posts From the Front Page

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.

Custom Field EditorDo 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:
  1. <?php while (have_posts()) : the_post(); ?>

Right below that, add a new line of code. The result will look like this:

PHP:
  1. <?php while (have_posts()) : the_post(); ?>
  2.    <?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:
  1. <?php endwhile; ?>

Right before that line, let's close that if statement we just added:

PHP:
  1. <?php } ?>
  2. <?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:

Possibly Related posts:

  1. WordPress: Fixing Bad Feeds
  2. Second Release: Add Related Posts to Your Feed
  3. Testing New WordPress Versions Part 4: Upgrade!
  4. Testing New WordPress Versions Part 3: The Files
  5. An Annoying Little Bug – Solved 9 months later!

About Chris

I consider myself a jack-of-all-trades. Which means I know a little about a lot... or think I do, at any rate. Check my "About Me" page for more info.
Tagged , , , , . Bookmark the permalink.