Basically we're (ab)using Category to determine whether a post is a News post or a Blog post, and so it's not a category in the traditional sense to the outside world. I'd like the category to not be indexed by feed-readers/crawlers, only the tags.

I found an easy way to do it with an easy change to 1 function in the core, but I'd rather avoid that if possible.

Is there a way to do that without modifying core? (Specifically get_the_category_rss in wp-includes/feed.php)

EDIT: What I mean specifically is to not list the category in the feed. For example, this is what part the current RSS2 feed looks like:


"blog" is a Wordpress Category, while "test", "testing", and "yeah!" are Wordpress Tags.

What I want is to exclude the entry <category><![CDATA[blog]]></category> entirely from the feed. The same should apply to Atom feeds and any others.

  • Do you want to prevent indexing of category feed by services only or kill this part of functionality completely? – Rarst Dec 16 '10 at 20:14
  • Well, as far as I can tell in the feed output it looks like it's treating the category the same way it's treating tags. Basically in the feed I don't want the category to be outputted. Category in our case doesn't carry the same semantic meaning as normal categories. – Davy8 Dec 16 '10 at 20:23
  • Yes, I understand. I ask what exactly do you want to happen - category links to not being displayed on front-end, category feeds contain no items, category feeds result in 404 error, something else? – Rarst Dec 16 '10 at 20:38
  • Tried to clarify what I meant in the edit. Let me know if that's not clear enough. – Davy8 Dec 16 '10 at 20:46

Tricky. It lumps categories and tags together pretty good. Had couple of approaches here is least messy:

add_filter('the_category_rss', 'remove_rss_categories');

function remove_rss_categories( $the_list ) {

    $categories = get_the_category();

    foreach ($categories as $category)
        $the_list = str_replace("<category><![CDATA[{$category->name}]]></category>", '', $the_list);

    return $the_list;
| improve this answer | |
  • Good call on add_filter. I didn't know about that. – Davy8 Dec 16 '10 at 22:50

@Rarst got me on the right track. I modified it to work on all the different types of feed with some copy/paste/modify from the exact code used in the WP core to make sure escaping is done in the same way etc.

Please give @Rarst the +1's, as it's really his solution. Just posting this as a more complete solution. I added this to my theme's function.php file and it did the trick.

NOTE that this could break in future if WP changes the format of the feeds. This is working as of WP 3.0

add_filter('the_category_rss', 'remove_rss_categories', 10, 2); 

function remove_rss_categories( $the_list, $type ) {
    if ( empty($type) )
      $type = get_default_feed();

    $categories = get_the_category();    
    $cat_names = array();

    $filter = 'rss';
    if ( 'atom' == $type )
      $filter = 'raw';

    if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
      $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
    $cat_names = array_unique($cat_names);

    foreach ($cat_names as $cat_name) {

      if ( 'rdf' == $type )
        $the_list .= str_replace("\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n", '', $the_list);
      elseif ( 'atom' == $type )
        $the_list .= str_replace(sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( apply_filters( 'get_bloginfo_rss', get_bloginfo( 'url' ) ) ), esc_attr( $cat_name ) ), '', $the_list);
        $the_list = str_replace("\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n", '', $the_list);
    return $the_list;
| improve this answer | |
  • One note from quick look, change first line to add_filter('the_category_rss', 'remove_rss_categories', 10, 2); because you need two arguments ($the_list, $type) to be passed to function and it defaults to one. – Rarst Dec 16 '10 at 23:03
  • Ah thanks. It seemed to work without it, but I guess it's only used to determine the type of sanitation to use, so it could very well be coincidence that it worked. Thanks. – Davy8 Dec 17 '10 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.