How To Exclude Posts With a Certain Tag From The Loop

Snippets by IsItWP

Are you looking for a way to how to exclude posts with a certain tag from The Loop? This snippet allows you to exclude posts with a particular tag from The Loop.


  1. Add this code to your theme’s functions.php file or in a site-specific plugin. It accepts the tag name and returns id that belongs to the tag for you.
  2. // The following function goes into your functions.php
    function get_tag_id_by_name($tag_name) {
        global $wpdb;
        return $wpdb->get_var("SELECT term_id FROM ".$wpdb->terms." WHERE `name` =  '".$tag_name."'");
  3. Add this code to The Loop.
  4. // Query_Posts in your loop
          'tag__not_in' => array( get_tag_id_by_name('teaser') )

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 9 Best SEO Tools to Grow Your Website Traffic, FAST!

Comments   Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!