Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thegulshankumar/de83fb8ee2ee300a4ee789be499c999b to your computer and use it in GitHub Desktop.
Save thegulshankumar/de83fb8ee2ee300a4ee789be499c999b to your computer and use it in GitHub Desktop.
Exclude Posts from Any Specific Category from RSS Feed in WordPress
/**
* Exclude Posts from Any Specific Category from RSS Feed in WordPress
*
* Use Case:
* This function is designed for a scenario where you manage a website that publishes content
* across ten different categories, but one specific category contains sensitive or restricted content.
* To comply with your content distribution policy, you may wish to exclude that particular category
* from appearing in the RSS feed.
*
* Solution:
* The function below modifies the WordPress query before the RSS feed is generated, thereby
* preventing posts from the specified category from being included in the feed.
*
* @param WP_Query $query The WP_Query instance (passed by reference).
* @return WP_Query The modified query.
*/
function exclude_categories_from_rss_feed( $query ) {
if ( $query->is_feed() ) {
// Specify the category IDs to exclude (replace 5 with your actual category ID)
$excluded_categories = array( 5 );
$query->set( 'category__not_in', $excluded_categories );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_categories_from_rss_feed' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment