Include or Exclude Category for RSS Feed or Search

16

Posted by: Jennifer Zelazny on June 16, 2010

Categorized: HOWTOs, SDAC Blog, WordPress

Tagged: , , , ,

There are certain times when you need to include or exclude certain categories in the feed or search results. The easiest way to take care of this is to add the following to your functions.php file.

function sdac_remove_from_feed( $query ) {
	if ( $query->is_feed ) {
		$query->set( 'cat',-1 );
	}
        return $query;
}
add_filter( 'pre_get_posts','sdac_remove_from_feed' );

The code above will exclude all posts that are in the category with ID 1 from your feed. If you wanted to exclude everything from the search results – you would change one line:
$query->is_feed to $query->is_search

If you wanted to only show items from the category with ID 1 – you would just need to remove the “-” before the category number.

Note: You can include/exclude items tagged, etc by changing the ‘cat’ to whatever else you would normally use in the query.

16 Responses to “Include or Exclude Category for RSS Feed or Search”

  1. ?? ??????????? PV…

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  2. ?????????? ???3?????????…

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  3. girl2u: ????????????????????????…

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  4. asiangoddess: kingreoo: bingo-bongo: godpaloma: tumugin: ……

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  5. (via hageo, knee-socks)…

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  6. bingo-bongo: nm44sp: (via shide, invasian)…

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  7. bingo-bongo: godpaloma: xyz: Feel It ?……

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  8. hageo: yrd: st250: bingo-bongo: ennui-time: munyunyu: ……

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  9. bingo-bongo: godpaloma: xyz: Feel It ?……

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  10. bingo-bongo: kamoseiro: frenchmilk: pruebame: galgal: a……

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  11. hageo: yrd: wettalk: sorshi: egyo: dametunes: via……

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  12. (via bingo-bongo, icore7)…

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  13. nm44sp: via blog-imgs-46-origin.fc2.com…

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  14. teoero: ???? (via nomore-a-girl)…

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  15. nm44sp: via livedoor.2.blogimg.jp…

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)

  16. Tibi says:

    Excellent tutorial man. I just implemented this on my blog so i can skip some categories from getting parsed on my RSS.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>