I love to write about tech tips and web development but also appreciate the fact that I love to keep a weblog of my daily activity and life here online. But that would contradict with my reader’s best interests to learn a new trick or two from this blog. So the best solution was to exclude certain categories from showing up in the wordpress homepage (index.php) and rss feeds. After a few investigations, here are a few ways which you use to get that done.
Method 1: Custom Feed URL
It’s really simple to tell WordPress to generate feeds for a specific category. For example, if I want to have a feed for the category WordPress and Tech Tips only, I could build a feed url such as:
<a href=”http://www.davidtan.org/feed?cat=6&cat=7″>Wordpress and Tech Tips (RSS)</a>
Similarly, to have a feed which doesn’t include both the WordPress and Tech Tips category, I could use the feed url (notice the extra minus signs):
<a href=”http://www.davidtan.org/feed?cat=-6&cat=-7″>No WordPress and Tech Tips (RSS)</a>
But wait, where do you get those numbers? They are the category id and can be obtain from your wordpress admin > Manage > Categories section. Just mouseover the category name link and you’ll see that the web address will be refering to something such as:
http://www.davidtan.org/wp-admin/categories.php?action=edit&cat_ID=7
Method 2: Add New Function in Theme’s functions.php file
Every wordpress themes comes with a functions.php file and you can add the following into it to exclude a certain category from showing up in the main index and rss feeds.
function excludeCategory($query) { if ($query->is_feed || $query->is_home) { $query->set('cat','-6, -7'); } return $query; } add_filter('pre_get_posts','excludeCategory');
Surprisingly I had a problem where if I use exclusions (indicated by the minus sign), there was a problem whereby old posts will be shown first on the blog. Not sure what the problem was but the solution will be to list every category id except for those which you want to exclude in the $query->set command. So for this example, it would be something like $query->set(‘cat’,’1, 2, 3, 4, 5, 8′);
Method 3: Feedburner Feed Details
If you’re using Feedburner, there’s a “Original Feed” field in the feed details section where it ask you to enter the feed url of your blog. Refering back to method 1, fill that field with a custom feed url with your excluded categories.
Method 4: WordPress Plugins
If you hate manual work and ain’t confident with editing functions.php, then this is the easiest way for you. I’ve found 2 wordpress plugins which helped in this matter: Advanced Category Excluder and SimplyExclude and I would personally recommend SimplyExclude.
Both plugins basically does what Method 2 can already do but you get more visual stuffs when configuring which category and in what sections (home, rss, archive, search) should those categories be excluded from showing up.
Again, like the post ordering problem we had in Method 2, you might also notice that you will get the “old post listed first” problem when using SimplyExclude. Here’s a user comment which helped in solving that problem.
If you select the “Exclude” option for the Front Page and check one or more categories in the corresponding column you will end up with inversed post order.
If however you select “Include only” for the Front Page and check all the categories you want to apear (which will probably be quite a few) it will work just fine (meanig you will have the normal post order).
That’s it, happy learning and tweaking WordPress! 🙂
Kirill says
Thankkkssss so much