6

I've been banging my head against the wall for last couple of days trying to exclude a category from an archive of easy digital downloads, which i am displaying in a custom widget, however i cannot hide a category called 'custom-project' no matter what i try.

This is the code i am trying to use, based on instructions from https://codex.wordpress.org/Class_Reference/WP_Query

$argsQuery = array(
    'posts_per_page' => 3,
    'post_type' => 'download',
    'tax_query' => array(
        array(
            'taxonomy' => 'download_category',
            'field' => 'slug',
            'terms' => 'custom-project',
            'include_children' => true,
            'operator' => 'NOT_IN'
        )
    ),
);
$get_latest_downloads = new WP_Query( $argsQuery );                         
$i=1;
while ( $get_latest_downloads->have_posts() ) : $get_latest_downloads->the_post();

//WIDGET BODY CODE

$i++;
endwhile;

I also tried using 'cat' instead of 'tax_query', but with no success as the category 'custom-project' is still displaying inside the loop of posts.

$argsQuery = array(
    'posts_per_page' => 3,
    'post_type' => 'download',
    'cat' => '-5',
);
$get_latest_downloads = new WP_Query( $argsQuery );                         
$i=1;
while ( $get_latest_downloads->have_posts() ) : $get_latest_downloads->the_post();

//WIDGET BODY CODE

$i++;
endwhile;

I am certain that slug name and category ID are correct. Any kind of help is greatly appreciated.

1
  • it's wrong and must be 'operator' => 'NOT IN'. if you change NOT_IN to NOT IN the function will work well. Commented May 15, 2023 at 9:51

2 Answers 2

10

Issue 1

In your tax query, you should use NOT IN instead of NOT_IN. That's preventing your tax query from working ( Assuming that the other fields are correct ).

Issue 2

In your arguments for WP_Query(), you should use category__not_in instead of cat. So, change your code to:

$argsQuery = array(
    'posts_per_page'   => 3,
    'post_type'        => 'download',
    'category__not_in' => 5 ,
);
0
8

https://codex.wordpress.org/Class_Reference/WP_Query

category__not_in (array) - use category id.

$argsQuery = array(
    'posts_per_page'   => 3,
    'post_type'        => 'download',
    'category__not_in' => array( 5 ), // array, not a string
);

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.