March 1, 2008
Excluding Posts from Your Wordpress Feed
“rabbit rabbit“. Happy March.
While it’s not my primary blogging subject, I’ve got a major subculture here around the subject of DIY electronics repair. My Samsung DLP television is my usual guinea pig, and I get a decent amount of traffic on that topic.
Naturally, I want to write more about that and I have plans to do so, but annoying my regular readers, the people I know personally, with that stuff has always kept me in check.
I discovered this morning that I can write about that stuff and keep those posts out of my regular feed as well as keep them off the homepage if I wish.
It’s quite simple to do so. Just add the following code to your template’s functions.php file:
function myFilter($query) {
if ($query->is_feed) {
$query->set('cat','-5');
}
return $query;
}
add_filter('pre_get_posts','myFilter');
That will keep category with ID 5 out of the feed. You can get the IDs by looking in the wp_terms table in your database. With wordpress 2.1, these terms are tags and categories, and while I haven’t tested it with tags, I have seen that it does work with Categories.
If you want to exclude more than one category, put them in separated by commas ‘-5,-10,-11′
This is a great solution if you’ve got some test posts or otherwise stuff you want to post but not hit the RSS. For example, if you’re pulling all your twitter posts into your blog, you can avoid cluttering up your RSS with this info.
Also, if you want to keep posts off the homepage, you use if ($query->is_home) instead of is_feed. Or you can do both with this: if ($query->is_home || $query->is_feed)
Thanks to Zeo for this great info.
If you enjoyed this post, please subscribe to my RSS feed
This is good. I was looking for something like this for quiet some time. Thanks.
What about the readers that would be interested just in those posts? Just relying on SE to index those pages?
Good question Vlad. I’m not sure, but I think you can get feeds on specific categories from wordpress. I’d have to research that a bit.
Please update here if you find a solution. I am working on something similar.
Can’t you set up seperate feeds for seperate categories? There may not be a plugin to do it easily, but it should be doable.
for category feed you must use this : http://example.com/category/categoryname/feed more information here : http://codex.wordpress.org/WordPress_Feeds
thanks for the info !! i will try it ….
@desing hotels-??? (cute name
)
Your suggestion will only work if you decide exclude posts on the home page of the blog. The code that Scot is going to use is placed in functions.php - I am afraid that it will either give an error or 404 page for the path “/categoryname/feed”
@Vlad glad you like my name
You’ve probably right about the 404 page… I’ll try this on a test blog.
@design hotels
I am interested in this solution. Currently I have a category on one of my wordpress sites where I exclude certain category from the front page but not from the main feed. I did not have time to try if modifying functions.php will affect the “category feed”
I didn’t want to exclude a whole category from my feed, so I made a tag to add to entries I didn’t want in the feed instead. I changed “cat” in the code you gave to “tag” (and the ID, obviously), and it works perfectly - thank you for that!
when I try this trick under WP 2.5 with feeds I get all the post in REVERSED order! I think it is because of the new RANDOM order introduced with 2.5. So far I was not able to come with any solution… to set query['order'] as well doesn’t work.
Nice trick, thanks for that, I’ll be using that in my sites to lop off spurious content from the feeds…
[...] Via Jangro [...]
[...] Excluding Posts from Your Wordpress Feed That will keep category with ID 5 out of the feed. You can get the IDs by looking in the wp_terms table in your database. With wordpress 2.1, these terms are tags and categories, and while I haven’t tested it with tags, I have seen that it does work with Categories. [...]