scottoliphant
Member
Posted 3 years ago #
I've searched hi and low for a way to run a query_posts and exclude posts with a specific tag. The way I'm using: Say i have a list of clients or work in a portfolio situation. I'd love to just be able to add the tag "archive" and have it hidden from my main query. What i've resorted to is this:
$posts = query_posts($query_string . '&tag=current+current&orderby=title&order=asc&posts_per_page=-1');
So now i tag everything with "current", instead of just tagging the archives with "archive". It works, but it's kind of heavy handed and would love a more elegant solution. Thanks in advance
scottoliphant
Member
Posted 3 years ago #
figured it out, an array couple with the "tag__not_in" was the trick
<?php
// remove clients tagged archive (42)
$my_query = new WP_Query(array(
'tag__not_in'=>array('42'),
'category_name'=>'Clients',
'orderby'=>'title',
'order'=>'ASC'));
} ?>
the loop
<?php while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
kasperbs
Member
Posted 3 years ago #
I found this very useful. Just to aid others who might have problems with the above code. You have to remove the last instance of '}'.
The code should look like this:
<?php
// remove clients tagged archive (42)
$my_query = new WP_Query(array(
'tag__not_in'=>array('42'),
'category_name'=>'Clients',
'orderby'=>'title',
'order'=>'ASC'));
?>
MarcusJames
Member
Posted 2 years ago #
This is what I used, partially from http://codex.wordpress.org/Template_Tags/query_posts. For mine I needed to include a specific tag and exclude another tag.
<?php
$args=array(
'showposts'=>10,
'offset'=>11,
'tag__in' => array('21'),
'tag__not_in' => array('20'),
);
query_posts($args);
while (have_posts()) : the_post(); ?>
STUFF
<?php endwhile; ?>
I am trying the same thing, but using the 'tag__in' => array('21'),
'tag__not_in' => array('20'), type code does nothing. Does this usually work?
jamesstiff
Member
Posted 2 years ago #
This worked a treat: http://www.jepson.no/how-to-exclude-specific-tag-from-query_posts/
get_var("SELECT term_ID FROM $wpdb->terms WHERE name='featured'");
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'tag__not_in' => array($tag),
'paged'=>$paged,
);
query_posts($args);
#
?>
I dropped the first line:
get_var("SELECT term_ID FROM $wpdb->terms WHERE name='featured'");
Just pasted everything from "$paged" into the desired page above
<?php if (have_posts()) : ?>
Replace "$tag" with the tag ID number.