One of the things that WordPress really does well in my opinion is generate feeds out of the box for posts, and even feeds for post categories are fairly easy to pull together, but today I was needing to put together an RSS feed from post tags. For those of you that might be unfamiliar w/ WordPress taxonomy, you basically have posts that are organized in categories but are refined more specifically using tags (or at least that’s how I teach my clients to leverage wordpress when publishing content, I have seen it done differently though, to each their own).
Since there really isn’t a good plugin to drop in and generate an RSS Feed from post tags, I had to resort to a hack (usually I’m not a fan of doing this). I went in and edited the wp-includes/feed-rss.php file and added the following lines of code just under the php header() call:
$tag = (urldecode($_GET['tag'])); if (!empty($tag)) { query_posts("tag=$tag"); }
Here’s what it looks like before:
<?php /** * RSS 0.92 Feed Template for displaying RSS 0.92 Posts feed. * @package WordPress */ header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true); $more = 1; ?> <?php echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
and here’s what it looks like afterwards:
<?php /** * RSS 0.92 Feed Template for displaying RSS 0.92 Posts feed. * @package WordPress */ header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true); $more = 1; /* generate rss feeds for tags */ $tag = (urldecode($_GET['tag'])); if (!empty($tag)) { query_posts("tag=$tag"); } ?> <?php echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
Once you have dropped this code into the feed-rss.php file and uploaded it to your wp-includes directory, you should be able to pull up an RSS feed based on tags by using the following URL structures:
- http://www.yourwebsite.com/feed/?tag=tag-name-here
- http://www.mysite.com/feed/rss/?tag=tag-name-here
Thanks to http://www.ebrueggeman.com/ for sharing this!!
Questions or Comments?