Prevent Posts in a Category From Displaying in WordPress’ RSS Feed

By  on  

I recently posted a snippet which prevented posts in a given category from displaying in WordPress' main loop.  I even went on to create my first WordPress plugin to accomplish the task via the WordPress control panel.  The one task those snippets doesn't complete is preventing posts from going into RSS feeds.  Here's how you can prevent posts in a category from displaying in the RSS feed!

The PHP

The category check is done the same way the loop check was done:

function postsFilter($query) {
    // Prevent from RSS feed
    if($query->is_feed()) {
    	// No posts in category #11 may go into the feed
    	$query->set('cat', '-11');
    }
}
add_action('pre_get_posts', 'postsFilter');

A pre_get_posts filter is added to check for the is_feed() return value.  If the value is true, we add the condition to remove the category form the final query.  I have added this functionality to the WordPress plugin as well!

Recent Features

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

Incredible Demos

  • By
    MooTools Documentation Search Favelet

    I'm going to share something with you that will blow your mind: I don't have the MooTools documentation memorized. I just don't. I visit the MooTools docs frequently to figure out the order of parameters of More classes and how best to use...

  • By
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

Discussion

  1. Ron

    Where do we add this php? I am a newbie

  2. Brilliant, thank you!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!