X

Exclude Categories from RSS Feed with functions.php

Snippets by IsItWP

Are you looking for a way to delete categories from RSS feed using your theme’s functions.php file? While there’s probably a plugin for this, we have created a quick code snippet that you can use to exclude categories from RSS feed with functions.php in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin. Then, add the IDs of the categories you wish to exclude on line 3 (currently set to 20, 21, and 22). Make sure to use a minus, “-“, before the category ID.

function exclude_cat_wps($query) {
    if ($query->is_feed) {
        $query->set('cat','-20,-21,-22');
    }
    return $query;
}
add_filter('pre_get_posts','exclude_cat_wps');

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: How to create a WordPress membership site and 9 best WordPress ad management plugins to boost revenue.

Comments  Leave a Reply

  1. I wonder if you can help, even though I have followed other instructions before I landed here.

    I have copied my blog so it is on 2 different pages on my website. On page 182 I want specific blog categories to show. On page 195 I want the rest of the categories to show.

    I created a child theme. Here I copied (through file manager) page.php and renamed one “page-182.php” and another “page-195.php”.

    In each page file I inserted a code which should include the specific categories for each page and saved it.

    But all categories show on both pages still, so nothing has changed.

    What am I doing wrong?

    This is page-195.php including the code:

    <div id=”primary” >

    $args = array(‘category__in’=>array( 90, 92, 91 ),);
    // The Query
    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) {
    echo ‘’;
    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo ‘’ . get_the_title() . ‘’;
    echo ‘’ . get_the_content() . ‘’;
    }
    echo ‘’;
    } else {
    // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

    —-this part is the code only:
    $args = array(‘category__in’=>array( 90, 92, 91 ),);
    // The Query
    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) {
    echo ‘’;
    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo ‘’ . get_the_title() . ‘’;
    echo ‘’ . get_the_content() . ‘’;
    }
    echo ‘’;
    } else {
    // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

    I hope you can see where I am going wrong. I am new and do not understand the code. Just google and youtubed my way through this.

    Thank you 🙂

    1. It appears that the PHP code may not be properly wrapped with PHP tags. You may also want to check out this snippet: https://www.isitwp.com/list-recent-posts-from-a-single-category/

  2. thank

  3. Does this take some time to update? I followed these instructions exactly but the category’s posts are still showing up in the RSS2.0 feed.

    1. Hi Zachary
      No this should work right away, do you have any other code that may cause this to have problems? RSS related plugins,

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!
[60% OFF Today!]
[60% OFF Today!]
[10% OFF Today!]
[10% OFF Today!]
[50% OFF Today!]
[50% OFF Today!]