0

I want to do a query to get posts with a slug and category__not_in. Here's what I do:

$query = array(
    "name" => "test",
    "category__not_in" => array( 15 )
);
query_posts( $query );

When printing the result on the screen, I get the given post - but it contains a category which I excluded. I want when I submit category__not_in it doesn't return any results, but it returns the post.

Post with slug test contains category 15, but I specifically say if the slug is test and there is no category 15, then show it to me. How can I tell WordPress to execute this condition?

  • Forgive me, but It's not clear to me what you're asking for. You wrote, "I want when I submit category__not_in it doesn't return any results, but it returns the post.". Huh? Can you explain further please? – jaswrks Nov 13 '17 at 7:15
1

Please read this Codex page why you should not use query_posts().

If you really want to use query_posts():

query_posts(
    'name' => 'test',
    'category' => '-15' // notice "minus 15"
);

The right way to exclude category:

$query = new WP_Query(
    array(
        'name' => 'test',
        'category__not_in' => array(15)
    )
);

if ( $query->have_posts() ) {
    .....
}
  • These scripts don't work. If i put only category, it works. If i add name it doesn't work. Any idea ? – Krasimir May 31 '16 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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