Exclude Specific Post, Category and Author from WordPress Feed

Print Page

WordPress LogoThere are many ways to disable feed for a WordPress website – with plugins, directly editing the core files, or by adding a small line of code to the functions.php file.

// Remove links to posts and comments feeds
remove_action( 'wp_head', 'feed_links', 2 );

// Remove links to category feeds
remove_action( 'wp_head', 'feed_links_extra', 3 );

Disable individual feed links:

function disable_feed_link($link) {
	return;
}
add_filter('category_feed_link','disable_feed_link'); // All categories feeds
add_filter('post_comments_feed_link','disable_feed_link'); // All posts comment feeds
add_filter('author_feed_link','disable_feed_link'); // All authors feeds

Now what if you don’t want to disable the entire feed but just exclude some specific stuff(s) from appearing in it? That’s what I am going to show you in this post. You would be able to exclude specific post, category and author from your website feed without using any plugin.

Note: All functions go in your theme’s functions.php file.

 

Exclude Specific Post from WordPress Feed

function wp_exclude_post($query) {
    if ( $query->is_feed) {
        $query->set('post__not_in', array(1) ); //Post ID
    }
    return $query;
}
add_filter( 'pre_get_posts', 'wp_exclude_post' );

Above function will allow you to exclude a specific post from your site feed. Replace the highlighted value with the post ID you wish to exclude. To exclude multiple posts, separate each ID with a “,” (comma).

$query->set('post__not_in', array(1, 2, 3) );

Exclude Specific Category from WordPress Feed

function wp_exclude_category($query) {
 if ($query->is_feed) {
   $query->set('cat','-1'); //Category ID
 }
return $query;
}

add_filter('pre_get_posts','wp_exclude_category');

Now with a slight change to the earlier function, this will allow you to exclude a certain category from your site feed. Again, you can specify multiple categories by separating each category ID with a comma. Do remember to add a “” (minus) sign before each category ID, else it will break your entire feed.

Exclude Specific Author from WordPress Feed

function wp_exclude_author($query) {
 if ($query->is_feed) {
   $query->set('author','-1'); //Author ID
 }
return $query;
}

add_filter('pre_get_posts','wp_exclude_author');

If for some reason, you don’t want a certain author’s posts to appear in the feed, you can do that with the above code. This function also lets you specify multiple authors.

Have fun.

4 thoughts on “Exclude Specific Post, Category and Author from WordPress Feed”

Comments are closed.