WordPress Tip: Custom RSS feed excluding one or more tags
The problem: I wanted to create an additional/alternative/extra RSS feed for a WordPress 2.7 blog that excluded posts with a given tag.
The reason there's not a straightforward way of handling this problem is because WP inexplicably doesn't offer any built-in tags/filters for excluding tags for a query the way it does with categories; plus there's no real built-in feed management to speak of.
The solution turned out to be remarkably simple, but it took about 2 hours of googling to figure it out (since "tags" and "rss" appear on every WordPress blog in existence). So I'm recording it here for any future searchers.
This is a two-step process: First you have to create the extra feed, then you have to modify its output. (Note: If you're looking to exclude posts with a certain tag from all of your feeds, this is not the place for you.)
Let's dive in.
- Download, install, and activate Ephram Zerb's Feed Wrangler plugin using the usual methods. This is what we use to create additional feeds.
- After activation, find Feed Wrangler in the Settings tab of your WP admin area. Use the form provided to create your feed.
- Feed Slug is how you'll access your feed: depending on how your permalinks are set up,
http://yoursite.com/feed/feed-slug/orhttp://yoursite.com/?feed=feed-slug - For the purpose of this tutorial, let's call the new feed
custom-feed, which will be accessed athttp://yoursite.com/feed/custom-feed/
- Feed Slug is how you'll access your feed: depending on how your permalinks are set up,
- After adding your feed, the plugin will display the name of an optional template file under "Template Being Used." It'll be in the form of
feed-custom-feed.php. Create a new file in your theme's directory with this name. For example:/wp-content/themes/your-theme/feed-custom-feed.php - Find the following file in your WP installation:
/wp-includes/feed-rss2.phpand copy and paste its contents into your newfeed-custom-feed.phpfile. - Verify that your feed is working by navigating to
http://yoursite.com/feed/custom-feed/. It should look precisely the same ashttp://yoursite.com/feed/ - Now, we have to modify the feed. We do this by changing which items are pulled from the database to populate the feed. First, find the numerical ID of the tag you wish to exclude.
- In your WP Dashboard, go to Posts -> Tags. Click on the tag you're excluding. The tag ID will be at the very end of the URL.
- Open up your new
feed-custom-feed.phptemplate in your favorite text editor. - Find the line that reads
< ?php while( have_posts()) : the_post(); ?>(line 34 in WP 2.7). - Replace that code (opening and closing php tags included) with the following code, where
$tagis set to the tag ID you found above. After replacing the code, save your template and you're done!
< ?php
$tag = "12";
$my_query = new WP_Query(array('tag__not_in'=>array($tag) ) );
while ($my_query->have_posts()) : $my_query->the_post();
?>
(Note: Be sure to remove the space in the opening PHP tag -- I had to put it there for formatting.)
If you're comfortable writing custom queries, you can go wild adding additional selectors; likewise, you could probably select multiple tags to exclude. But all of that is beyond the scope of what I need right now, and therefore beyond the scope of this post. /smile
Note: If you're using FF on a Mac to preview your feed, you'll need to force-reload the URL (command-shift-R) to see the changes. (This may be true of other browsers/platforms, as well.) Verify that it's all working correctly by checking your feed in a dedicated aggregator.
I hope this helps some poor soul wandering in the google hinterlands. Feel free to ask questions; I'll do my best to answer them.
1 Comment