Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Exclude Posts from WordPress Blog or RSS Feed Based on Tag

Further to our snippet on removing a category from an RSS feed, this snippet will exclude posts with a specific tag from either showing up in your blog or RSS feed.

The first snippet will prevent a post from showing up on your WordPress website.

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
    Exclude Posts from WordPress Blog or RSS Feed Based on Tag
4
    http://www.beliefmedia.com/code/wp-snippets/exclude-posts-wordpress-tag
5
*/
6
 
7
 
8
function exclude_post_tag($query) {
9
  if ( $query->is_home) {
10
   $query-> set('tag__not_in',array(3));
11
  }
12
 return $query;
13
}
14
add_filter('pre_get_posts','exclude_post_tag');

If you wanted to exclude multiple tags from showing up, you would do so as follows: $query-> set('tag__not_in',array(3, 4, 5, 6)); and so on. Keep in mind that the numbers relate to the tag ID that you can identify by hovering your mouse over the tag in your WordPress control panel.

Using a variation of the code above, you can easily exclude posts from showing in your WordPress RSS feed. An RSS feed is often considered to be 'selective content', and many bloggers wish to control what information is made available outside of their website. You can accomplish this using the following code.

1
<?php 
2
/*
3
    Exclude Posts from WordPress Blog or RSS Feed Based on Tag
4
    http://www.beliefmedia.com/code/wp-snippets/exclude-posts-wordpress-tag
5
*/
6
 
7
function exclude_tag_rss($query) {
8
  if ( $query->is_feed) {
9
   $query-> set('tag__not_in',array(34));
10
  }
11
 return $query;
12
}
13
add_filter('pre_get_posts','exclude_tag_rss');

You can remove multiple tags from your RSS feed using the method described above.

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment