Showing Taxonomy Terms in RSS Feed

The problem: I have a custom post type "announcements" and a custom taxonomy "announcements_taxonomy". I can access the rss feed by going to http://domain.com/announcements/feed - The problem is I am can not see the categories/tags in the RSS feed.

Detail: I am using Simplepie to get the feed items and show them outside of a WordPress install. Because the rss feed on my WP site does not show the categories I can not access them with Simple pies get_categories() function.

Question: How can I get categories from my custom post type and taxonomy to show in the rss feed?

Topic rss Wordpress

Category Web


Here is how to show taxonomy terms in rss feed. Just add this to the functions.php file and update the $tax and $post_type variables to match your needs.:

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 = 'announcement_tags';
$post_type = 'announcements';

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

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

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

if(!empty($categories))
{
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;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.