How to not treat categories as tags in feeds

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.

Read More

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:

<category><![CDATA[blog]]></category>
<category><![CDATA[test]]></category>
<category><![CDATA[testing]]></category>
<category><![CDATA[yeah!]]></category>

“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.

Related posts

Leave a Reply

3 comments

  1. 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;
    }
    
  2. @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("tt<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);
          else
            $the_list = str_replace("tt<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>n", '', $the_list);
        }
        return $the_list;
    }
    
  3. I made category removal with such code in functions.php:

    add_filter( 'the_category_rss', 'remove_category_from_rss', 10, 2 );
    function remove_category_from_rss( $the_list, $type ){
        return;
    }