WordPress.org

Ready to get started?Download WordPress

Forums

Exclude category from the main RSS feed and also keep that category feed (3 posts)

  1. acosmin
    Member
    Posted 4 months ago #

    If you want to exclude a category from the main RSS feed and also keep that category rss feed use the following code:

    function ac_exclude_from_rss($query) {
        // Categories to exclude - by ID
        $cats_to_exclude = array( 15, 16);
    
        if ($query->is_feed && !$query->is_category($cats_to_exclude) ) {
            set_query_var('category__not_in', $cats_to_exclude);
        }
    
        return $query;
    }
    add_filter('pre_get_posts','ac_exclude_from_rss');

    If someone has something to add please share :)

  2. Ashok
    WPMU DEV Support Staff
    Posted 4 months ago #

    How about this one?

    function exclude_category($query) {
     if ($query->is_feed) {
       $query->set('cat','-14,-9');
     }
    return $query;
    }
    add_filter('pre_get_posts','exclude_category');
  3. acosmin
    Member
    Posted 4 months ago #

    Yes, that works if you want to exclude some categories from http://yourwebsite.com/feed. It will also disable http://yourwebsite.com/category/acategory/feed which I didn't wanted.

    My code works in both cases.

    I forgot to mention it was a follow up for this thread.

Reply

You must log in to post.

About this Topic