Archive for the ‘how to’ Category

Exclude Posts – Update

June 8th, 2009

OK – so after doing the update in order to exclude my weekly twitter digest, I noticed some problems. The main one was that the ‘Older Entries’ link didn’t work. I tried several variants, but the only way I could get them to work again was by deleting the exclude code I had put before the loop. So I deleted it.

I assumed that it was because the is_home() and is_front_page() conditional tags weren’t working properly, so I checked out the loop page again. And I found this example, which works exactly as I want it too. It doesn’t answer the questions of whether or not the conditional tags for home and front page actually work – as a matter of fact, even when you’re not on the homepage (front page) the posts from my tweets category still get filtered out. This is OK for me because that’s what I want to happen; however, if you want something to happen on the homepage (front page) only, I would look for another way.

Exclude Posts Belonging to a Category

June 6th, 2009

UPDATE 06/08/09
This didn’t work out the way I planned, so I had to do something else


OK – so I have this nifty plugin that lets me post new blog entries to my twitter profile (Twitter Tools). The plugin also posts a weekly digest of my tweets. The problem is that I don’t want the weekly digest posted to my homepage; however, I do want the weekly digest posted to the blog, so I needed to find a way to filter a category from my homepage.

I searched wordpress.org support and found several plugins that sort of did what I wanted, but it seems ridiculous to install a plugin for something as simple as filtering a query, so I continued to search and found a helpful list of Query Posts that would handle the filtering. I then had to place the code before the WordPress Loop, so the category would be filtered out before the loop started. Here’s what I ended up using:

<?php
if (is_home()) {
query_posts("cat=-3");
}
?>

where 3 is the ID of the category you want to exclude from the page – and don’t forget the minus sign (-) before the ID number.

I opened the index.php file in a text editor, but you can use the built-in WordPress theme editor or file editor if you wish. I then had to place the code before the loop began:

<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<?php
if (is_home()) {
query_posts("cat=-3");
}
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">

You’ll need to know the ID of the category you wish to exclude in order to do this. To find the category ID from the Dashboard, go to Posts → Categories and hover over the category name. When you do this the link address will appear in the bottom left of your browser (or wherever the onHover link address appears in your browser). At the end of the address you’ll see …cat_ID=3 – whatever the number, that’s the ID for that category.

It didn’t take me long to find all this information, but I had to look in several places for the entire process, so I hope this saves someone some time.

Things to understand:
With this method you can only exclude posts that belong to one category. If a post belongs to two categories, it will still be posted.
This method addresses posts to the homepage. The posts will still appear on the category’s page, and the category listing will still appear in the sidebar.