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.
Instructions:
- 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.
- Add this code to The Loop.
// 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."'"); }
// Query_Posts in your loop query_posts( array( '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