Excluding a blog category from your feed

While I was working on a new system for my WordPress newsletter, a system that uses its own custom feed (skipping FeedBurner which caused errors last week), I had to figure out how to exclude a category from my blogs regular feed as well as my homepage and some other pages.

There’s a couple of ways to go about this, but what I decided to do was the following:

function mail_feed_query($query) {
	if ($query->is_home || $query->is_front_page) {
		$query->set('cat','-120');
	}
	return $query;
}
add_filter('pre_get_posts','mail_feed_query');

(120 is the category ID here)

This filters out the query from the home page and/or front page of your blog. Now you’ll need to do a bit of extra work to prevent it from showing up in category listings, everywhere you do a get_categories or wp_list_categories you just add &exclude=120 (where, again, 120 is the category ID).

So as of next week, my WordPress newsletter will be having its own unique articles again, as well as all the articles in the WordPress category. So, subscribe, like, now, so you don’t miss any of the goodness!

One of the things I intend to send out only to my newsletter subscribers is the new plugins I come across that I like or find useful, so if you don’t subscribe to the newsletter, you’re really missing out.

Tags:


Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

4 Responses

  1. TheAnandBy TheAnand on 25 June, 2009

    So, why not use feedburner and this script for a newsletter plugin avoiding the aweber?

  2. Navin PoeranBy Navin Poeran on 26 June, 2009

    @TheAnand: that’s also a nice way of sending newsletters, for free, without using aweber

  3. Esther NowackBy Esther Nowack on 29 June, 2009

    Why not not just using if($query->is_feed) ?

    • OrenBy Oren on 30 June, 2009

      Haha, this guy got a point here.
      Waiting for Joost de Valk’s comment now :)