WordPress has many alternate built-in feeds: per category, per tag, per author, per search-keyword. But in some cases, you want feeds built with some more advanced logic. Let’s look at the available options.
WordPress advanced built-in feeds
You can create feeds for “unions” or “intersections” of tags, you just have to use a URL like /tag/foo,bar/feed/
(all articles tagged with foo or bar) or /tag/foo+bar/feed/
(all articles tagged with foo and bar).
You can also have feeds excluding a category, although that requires you to know the category identifier (and hardcode it in the URL like this: /?feed=rss2&cat=-123
where 123 is the category id that you want to exclude).
But there’s no simple way to have a feed that excludes articles with a given tag. The best solution found involves creating a custom feed. I’ll show you a variation of this below.
Creating a custom feed
- First of, install the Feed Wrangler plugin, it will take care of registering our custom feeds with wordpress.
- Go to “Settings > Feed Wrangler” in your WordPress administrative interface and create a new feed, let’s call it “myfeed”.
- You should now create a “feed-myfeed.php” file and put it in your current theme’s directory. The initial content of that file should be this:
<?php include('wp-includes/feed-rss2.php'); ?>
- At this point, you already have a new feed that you can access at
/feed/myfeed/
(or/?feed=myfeed
). It’s a complete feed like the main one.
Now, we’re going to look at ways to customize this feed. We’re going to do this by changing/overriding the default query that feed-rss2.php’s loop will use.
A feed excluding articles with a tag
If you want to create a feed that excludes the tag “foo”, you could use this:
<?php global $wp_query; $tag = get_term_by("slug", "foo", "post_tag"); $args = array_merge( $wp_query->query, array('tag__not_in' => array($tag->term_id)) ); query_posts($args); include('wp-includes/feed-rss2.php'); ?> |
That was relatively easy, thanks to the “tag__not_in” query parameter. Now you can further customize the feed by adding supplementary query parameters to the $args array. The documentation of query_posts details the various parameters that you can use.
A feed excluding articles with a custom field (meta-data)
I went further because I did not want to use a tag to exclude some posts: that tag would have been public even if it was only meaningful to me. So I decided to use a custom field to mark the posts to exclude from my new feed. I named the field “no_syndication” and I always give it the value “1”.
This time it’s not so easy because we have no query parameter that can be used to exclude posts based on custom fields. We’re going to use the “post__not_in” parameter that can be used to exclude a list of posts. But we must first generate the list of posts that we want to exclude. Here we go:
<?php global $wp_query; $excluded = array(); $args_excluded = array( 'numberposts' => -1, 'meta_key' => 'no_syndication', 'meta_value' => 1, 'post_type' => 'post', 'post_status' => 'published' ); foreach (get_posts($args_excluded) as $item) { $excluded[] = $item->ID; } $args = array_merge( $wp_query->query, array('post__not_in' => $excluded) ); query_posts($args); include('wp-includes/feed-rss2.php'); ?> |
A feed with modified content
You might want to add a footer to the articles that are syndicated. I use the Ozh’ Better Feed plugin for this but it applies to all your feeds.
You could do that sort of transformation only in your customized feed by using the WordPress filter named the_content_feed.
Here’s a simple example:
<?php function myfeed_add_footer($content) { return $content . "<hr/>My footer here"; } add_filter('the_content_feed', 'myfeed_add_footer'); include('wp-includes/feed-rss2.php'); ?> |
I’ll stop here but obviously you have lots of options and many ways to tweak all the snippets above. They have been tested with WordPress 3.0.4.
Note that in all those examples, I took care to not duplicate the code from feed-rss2.php, instead I used include() to execute it. That way my custom feeds will automatically benefit from all the future enhancements and fixes made by the WordPress developers.
But if you have to modify the XML structure of your custom feeds, you can paste the content of feed-rss2.php in your file and change it like you want…