How-to-exclude-a-category-from-your-wordpress-feed

I have a question about this post:
https://premium.wpmudev.org/blog/how-to-exclude-a-category-from-your-wordpress-feed/

I've followed the instructions in that post and created a plugin on my multisite network. activated it on one of my subsites. but the categories being excluded are still showing in the feed.

I was provided with this code below but didn't work:

function wpmu_exclude_category($query) {
if ( $query->is_feed ) {
$query->set('cat', '-44, -45');
}
return $query;
}
add_filter('pre_get_posts', 'wpmu_exclude_category');

I've also enabled support access, can you help?

  • Ken Kimbrell

    Hello BW-

    I understand you are trying to exclude a specific category from your wordpress-feed. I recommend this plugin, Ultimate Category Excluder It has worked great for me in the past, and seems to work well in a multisite environment. Give it a shot, if it does not deliver your desired results. Then I can work on a custom function for you :slight_smile:

    Let me know how this works for you!

    Take Care,

    Ken - WPMU DEV Support

  • Ken Kimbrell

    Hello BW-

    I am going to share with you how I was successful in achieving this type of functionality. You are going to want to go into your root directory where your core wordpress installation resides. Then you need to find wp-content, within this directory, you will need to make a new directory called mu-plugins, this is a special folder that stands for must use, and can be activated across all multisites. You can read more about it here: mu-plugins

    Then within this mu-plugins directory you are going to want to add a functions.php. This plugin does not need any kind of activation, it is automatically activated across all sites in the network and it can not be removed by your clients, nor can they even see it. Provided you don't add the commented out template needed for wordpress to recognize the function as a plugin and show it on the backend.

    So your root directory to the custom plugin should look like this: ../wp-content/mu-plugins/functions.php

    Within the functions.php you are going to want to apply this code:

    <?php
    
    // Exclude Category Posts by WPMU DEV support
    function wpmu_exclude_category( $query ) {
    	if ( $query->is_home() ) {
    	$query->set( 'cat', '-70' );//Choose the category ID, e.g. -70
    	}
    	return $query;
    }
    add_action( 'pre_get_posts', 'wpmu_exclude_category' );
    
    ?>

    Let me know if this helps!

    Take Care,

    Ken - WPMU DEV Support

  • Ken Kimbrell

    Hello BW-

    I do apologize for the delayed time in response. The code my colleagues and I provided to you does work. Problem is that the wordpress feed has quite the delay in updating the RSS feed. Meaning it could be anywhere from 30-60min before you see the changes take place. I tested this theory out by changing the post title, and you can see in the feed, the post will have the old post title. You just need to be patient and wait for the RSS feed to update. Of course, I also looked into a way of altering the refresh rate of the RSS feed and came up with the following solution. Please add this code in the mu-plugins/functions.php as described before:

    // Increase refresh time of RSS feed
    add_filter( 'wp_feed_cache_transient_lifetime', create_function('$a', 'return 600;'));
    
    // Exclude Category Posts by WPMU DEV support
    function wpmu_exclude_category( $query ) {
    
    	if ( $query->is_feed ) {
    
    	$query->set( 'cat', '-12, -13, -14' );//Choose the category ID, e.g. -12
    
    	}
    	return $query;
    
    }
    add_action( 'pre_get_posts', 'wpmu_exclude_category' );

    You will want to change the category IDs to your specific category IDs, there are a number of ways to determine the cat_id. One way is go to your wordpress dashboard, find default wordpress Posts > Categories, find the category you want to exclude from the RSS feed and hover over it, then in the url below you will see among the chaos tag_ID=63 (in my case) yours will appear differently depending on the category name you hover over and its associated tag_ID. See image below:

    As for the filter, we are modifying the refresh rate of the RSS Feed to 10 minutes. There are 600 seconds in ten minutes. So if 10 minutes is not soon enough for you. You can always modify that parameter as well, just know that the parameter is determined by seconds.

    Let me know if this helps!

    Take Care,

    Ken - WPMU DEV Support

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.