RSS Feed for posts containing any term from a taxonomy

I imagine the answer to this will be related to this question I asked yesterday:

Custom Taxonomy WP_Query for All Terms in a Taxonomy?

I would like to output an RSS feed containing all posts that have ANY entry from a particular taxonomy assigned to them. I can do this:

http://blog.example.com/feed/?taxonomy=taxonomy-name&term=test-term

To return all posts assigned to a particular term, but I would like to have a feed that shows all posts from all terms in that taxonomy.

Thanks,
Dave

Solutions Collecting From Web of "RSS Feed for posts containing any term from a taxonomy"

The Solution

WordPress 3.1 Taxonomy Feeds Plugin (Must Use)

Put that must-use plugin on your site and request:

http://blog.example.com/feed/?taxonomy=taxonomy-name

as you asked for it.

Detailed Information

WordPress offers requesting the feed with multiple configurations. Finding Your Feed URL (WordPress Codex) shows what is possible. Next to the standard feed (posts) you can have the following:

  • Comments – http://example.com/?feed=commentsrss2
  • Categories – http://example.com/?cat=42&feed=rss2
  • Tags – http://example.com/?tag=tagname&feed=rss2
  • Authors – http://example.com/author/authorname/feed/
  • Search – http://example.com/?s=searchterm&feed=rss2

(Note: RSS2 is WordPress’default feed format)

As you can see, there is no example URL offered that is about querying the feed for posts of a taxonomy.

In general, the Feed is just like any other blog post or comments line-up: a WP_Query and then running the loop in a feed template.

WP_Query has support for the taxonomy request parameter by default. That request parameter is sort of documented in codex, that section has two issues: 1.) it’s taxonomy (not tax) and second 3.1 is not released yet, so that’s not deprecated already. The point I link that document is to show that it’s value is compared against the taxonomies slug.

So to find out if WordPress could easily do what you want to do you need to find out if the feed is already supporting taxonomies when requesting a certain category. I did this already, this is not the case. An additional request query var is used instead: category_name.

So for the moment the only interesting part is that we can make additional use of the taxonomy request parameter and when wordpress looks for a feeds database data it is in some legacy category query mode, not in an taxonomy query mode. That’s a bit of a pity as you are looking for taxonomies.

Take a breath for the moment

Because you need. The problem here with WordPress is that you now start to run into the most complex inner guts of the software. There are some enormous complex functions in the package which to understand is like debugging code: You need to go through step by step.

While doing so you might be lucky enough to find a filter or hook you can make use of to manipulate values and callbacks in the go to make the thing do what you want to do. But the problem is, the more you tinker the higher is the risk to break things.

So the art is to find the place to make changes that is the less invasive location.

Adding support for Taxonomies

So as written above, support for taxonomies needs to be added in WP_Query directly. This can be done by hooking into that function and changing values while doing so.

But can be queried for taxonomies at all? I could not find much pre 3.1 information about that. It looks like that even WordPress 3.0 supports custom taxonomies, it was not possible to query posts that are in a specific taxonomy by request parameter.

Even in my 3.1 testbed I was not able to map request parameters to taxonomy queries. So even with 3.1 this might not be easily done.

The best looking solution I can see so far is to hook into WP_Query and make use of a WordPress 3.1 based custom taxonomy query, here some mock-up code:

$myquery['tax_query'] = array(
    array(
        'taxonomy' => 'category',
    ),
);
query_posts($myquery);

I smell that this might not work, because we query for taxonomy only. Let’s check that in detail.

WP 3.1 Custom Taxonomy Query for Taxonomy Only

To find out, I implement a simple script that hooked into pre_get_posts and injected the WordPress 3.1 Custom Taxonomy Query shown above. As taxonomy I used one of the default ones: category (You can find all default taxonomies here). It spit a lot of PHP warnings then which is a sign that this feature is somehow fragile. So be warned, don’t expect things to work out of the box.

Anyway I tested this and to make a long story ending quickies, I found the solution to just grab all terms and then add them to the query. Here is my mu-plugin I made use of so far: (posted above), does the job for me.