How to exclude a specific category from the RSS using a snippet

Last updated December 6th, 2023 00:01

RSS is a feature in WordPress. It allows readers to subscribe to and display updates from your website’s content using RSS readers. It’s like a subscription. It enables people to follow new articles, posts, or updates on your blog without the need to regularly visit your website. In WordPress, RSS generates a special file called a “feed” that contains a list of the latest posts and their content. This file structure is in XML format, and an RSS reader can actively read the file, monitor changes, and present new articles to readers. Readers can subscribe to updates on your website in the same way they receive emails in an email client. However, if you wish to exclude specific categories from the RSS feed, you can do so by using a short code. Here is a guide on how to exclude a specific category from the RSS using a snippet.

How to Exclude a Specific Category from the RSS Using a Snippet

For this snippet, it’s important to know the category ID that you want to exclude. You can find a complete guide on how to retrieve the category ID here: How to Find the ID of a Post, Page, Category, or Tag in WordPress? Insert the category ID(s) into the snippet, and it will ensure that articles in those categories are not included in the RSS feed.

WPCode Plugin

To add short PHP codes, also known as snippets, to your website, you will need a plugin. There are several available in the official repository. Personally, I recommend using the WPCode plugin. It can also be found at this address: https://wordpress.org/plugins/insert-headers-and-footers/

With this plugin, you can easily add code to the system that will run and perform actions and functions defined by you. For detailed instructions, if you have never worked with such a plugin before, you can find them here: How to Insert Custom Code in WordPress

If you have read the article, you probably already have an idea of how to add such a snippet to your website. The code for completely disabling comments looks like this:

				
					/**
 * Exclude a category or multiple categories from the feeds.
 * If you want to exclude multiple categories, use a comma-separated list: "-15, -5, -6".
 * Make sure to prefix the category id(s) with a minus "-".
 *
 * @param WP_Query $query The query.
 */
function wpcode_snippets_exclude_feed_category( $query ) {
	if ( $query->is_feed ) {
		// Replace 15 with the desired category id you want to exclude.
		$query->set( 'cat', '-15' );
	}
}

add_action( 'pre_get_posts', 'wpcode_snippets_exclude_feed_category' );
				
			

What do you need to modify in the snippet?

The line you will be most interested in is $query->set( ‘cat’, ‘-15’ );. The number 15 represents the category ID that you want to exclude from the RSS feed. You need to modify this number to the correct category ID. If you want to exclude multiple categories, you can modify the syntax as follows:

				
					$query->set( 'cat', '-15,-20,-25' );
				
			

As you can see, you separate the categories within quotation marks with just a comma, without any spaces. Make sure that each category is still enclosed in parentheses.

More content about WordPress

Whole category is here

The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.

Byl pro Vás tento článek užitečný?

Klikni na počet hvězd pro hlasování.

Průměrné hodnocení. 0 / 5. Počet hlasování: 0

Zatím nehodnoceno! Buďte první

Jak užitečný vidíte tento článek.

Sledujte mě na sociálních médiích.

Je mi líto, že pro Vás nebyl článek užitečný.

Jak mohu vylepšit článek?

Řekněte mi, jak jej mohu zlepšit.

newsletter

Subscribe to the Newsletter

Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.

Odebírat
Upozornit na
guest
0 Komentáře/ů
Vložené zpětné vazby.
Zobrazit všechny komentáře.

Pokud mi chcete napsat rychlou zprávu, využije, prosím, níže uvedený
kontaktní formulář. Děkuji.

Další Kontaktní údaje