Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

how to add description of custom taxonomy term to RSS Feed WordPress


I have a multisite install with one main site and 3 sub-sites together forming a network.

On the sub-site:
For a Custom Post Type called "Review" I have set up a couple of Custom Taxonomies. One of them is (author)"Rating" (a category-like taxonomy) and the 5 different terms are 1 star, 2 stars, 3 stars, 4 stars and 5 stars. These terms also have a description and for that description I use star images.
So far so good.

On the main site:
In the sidebar I want to show the 5 most recent Reviews of each of the 3 sub-sites. That's pretty easy by adding the feed of each of these 3 sub-sites into a widget. But what I so far have been unable to accomplish is to get the description of the terms of the Rating taxonomy to show up next to it.

For that I obviously first need to add the description of the terms of the custom taxonomy to the feed and then I need to extract it again from the feed.

Anyone that can help me with this, would be very much appreciated! If you need a better explanation, please let me know. I don't think having the url is necessary to solve this problem; if needed I can only give via private message.

Answers (3)


Jurre Hanema answers:

To add the terms of a custom taxonomy to an RSS feed, try this:

add_filter('the_category_rss', 'wpq_the_category_rss');
add_filter('rss2_ns', 'wpq_rss2_ns');

function wpq_the_category_rss($the_list_original)
$tax = 'product_cat';
$post_type = 'product';

if(get_post_type() != $post_type)
return $the_list;

$categories = get_the_terms(get_the_ID(), $tax);

$the_list = '';
$cat_names = array();

foreach((array)$categories as $category)
$cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, $tax, 'rss');
$cat_descriptions[] = sanitize_term_field('description', $category->description, $category->term_id, $tax, 'rss');

$cat_names = array_unique($cat_names);
$cat_descriptions = array_unique($cat_descriptions);

foreach($cat_names as $cat_name)
$the_list .= "\t\t<category domain=\"". $tax ."\"><![CDATA[" . @html_entity_decode($cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";

foreach($cat_descriptions as $cat_description)
$the_list .= "\t\t<wpq:category_description domain=\"". $tax ."\"><![CDATA[". @html_entity_decode($cat_description, ENT_COMPAT, get_option('blog_charset')) ."]]></wpq:category_description>\n";

return $the_list_original.$the_list;

function wpq_rss2_ns()
echo "xmlns:wpq=\"\"\n";

As you can see in the function wpq_the_category_rss(), this function works for posts of type 'product' and for the taxonomy 'product_cat'. You can of course change these two to your liking.

The code adds a new element <wpq:category_description> containing the description of the term for each term the post is associated with. (I invented the wpq-namespace to keep the RSS valid and all).

Do you think this is enough or do you need more help with reading this data back from the feed? (if so, please post the code you are using now)

senlin comments:

Hi Jurre,
Your code sample works perfect and indeed adds the new element to the feed!
Yes, it would be really helpful if you can help with reading this data back from the feed too.
What I am using now to call the feed is a RSS widget (on the main site) where I use the feed-address of:
The output of the RSS now shows:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"

<title>name of subsite &#187; Reviews</title>
<atom:link href="" rel="self" type="application/rss+xml" />
<description>description sub site</description>

<lastBuildDate>Thu, 27 Oct 2011 07:28:20 +0000</lastBuildDate>
<xhtml:meta xmlns:xhtml="" name="robots" content="noindex" />

<title>Name Review</title>
<pubDate>Mon, 12 Sep 2011 13:04:42 +0000</pubDate>
<category domain="rating"><![CDATA[4 stars]]></category>

<wpq:category_description domain="rating"><![CDATA[<img src="" /><img src="" /><img src="" /><img src="" /><img src="" />]]></wpq:category_description>

<guid isPermaLink="false">;p=72</guid>
<description><![CDATA[<p>If you enjoy poker at all this game is fun, quick and addicting.</p><p><a href="">title review</a> has originally been posted on <a href="">name subsite</a></p>]]></description>
<content:encoded><![CDATA[<p>If you enjoy poker at all this game is fun, quick and addicting.</p><p><a href="">title review</a> has originally been posted on <a href="">name sub site</a></p>]]></content:encoded>


Jurre Hanema comments:

It turns out that you can not modify the output of Wordpresses built-in RSS widget, therefore it was needed to create a new widget with my modifications. I put the code here:

If you insert this code through your functions.php, you should end up with a new widget called "RSS (WPQ)". It works just the same as the default widget, except it also outputs the category name & description next to every item. I did not make this behaviour configurable, as that would require the copying and modification of even more code.

I don't know if this produces the exact output you want, but further modifications should be pretty easy. If you search for my comment "Modify final output here" you may even be able to figure it out for yourself.

senlin comments:

Geweldig Jurre! Enorm bedankt! Werkt prima en makkelijk zelf aan te passen (heb de cat naam weggehaald)

Fantastic Jurre! Thanks so much! Works great en easy to adapt (deleted the cat name)


Alex Sancho answers:

You can use a hook to inject your custom data inside each post on feed, the action is "rss2_item"

add_action( 'rss2_item', 'my_custom_function' );

senlin comments:

Thanks for your input Alex. Jurre however nailed it!


Luis Cordova answers:

just put it on a widget right or use samsarin php

senlin comments:

I am not sure whether you understand the question. The widget does not by default show the description of the custom taxonomy of the custom post type...