0

I want to get all the posts that do not contain the tag 'index'. How can it be done?

I mean in the reverse of

get_posts(array('tag' => 'index'))

Thank you

2 Answers 2

3

You could try the tax_query with the 'NOT IN' operator (untested)

$myposts = get_posts( 
    [
        'tax_query' => [
            [ 
                'taxonomy' => 'post_tag',
                'terms'    => [ 'index' ],
                'field'    => 'slug',
                'operator' => 'NOT IN',
            ]      
        ]
    ]
);

where the taxonomy slug for tags is post_tag.

2
  • 1
    Always +1 for using a proper tax_query, it is so much more flexible ;-) Commented Mar 30, 2016 at 13:45
  • yes the syntaxly correct tax_query is flexible ;-) thanks for fixing the silly mistake ;-) @PieterGoosen
    – birgire
    Commented Mar 30, 2016 at 13:53
0

Per WordPress Codex you should use a custom query for any of post types interrogations. Presuming your taxonomy is 'post_tag' instead 'tag' use the following code:

$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => array('index'),
'operator' => 'NOT IN' )));

$custom_query = new WP_Query( $args ); while($custom_query->have_posts()) { $custom_query->the_post(); //your custom code }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.