How to exclude specific tag from query_posts

Coding April 23, 2009 5 comments

On the upcoming theme I’m working on I needed to have a featured section on the left side, and normal posts on the right. Usually I would put the featured posts in a category, and excluded that from the normal posts. But for the end user, it is much easier to just tag the posts they want to be featured with “featured”. That way they don’t have to create a category called featured, select that in the options panel etc.

It isn’t so well documented,  so after talking to Adii we came up with the following code that does the job perfectly for the right hand side normal posts:

  1. get_var("SELECT term_ID FROM $wpdb->terms WHERE name=’featured’");                     
  2.  
  3. $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
  4. $args=array(
  5.    ‘tag__not_in’ => array($tag),
  6.    ‘paged’=>$paged,
  7.    );
  8. query_posts($args);
  9. ?>

The left side query is simple and just looks like this

  1.  
  2. <?php query_posts(‘tag=featured’); ?>
  3.  

Magnus Jepson

Hello! I’m Magnus Jepson, co-founder of WooThemes. See my business/work underneath or check my latest tweets on the right.