0

While this works:

$query->set( 'post__not_in', array( 30140, 30020, 30008, 29998, 29991, 21458, 20197, 11986, 6614 ) );

This doesn't:

$tax_query = array(
  'taxonomy' => 'filter',
  'terms'    => array(30140, 30020, 30008, 29998, 29991, 21458,20197,11986,6614),
  'field'    => 'term_id',
  'operator' => 'NOT IN');
$query->set( 'tax_query', $tax_query );
3
  • I'm not sure why you expect it to? post__not_in takes a list of post IDs, and returns posts that aren't in that list. tax_query queries posts based on which taxonomy terms they have. Nov 9, 2018 at 15:12
  • Humm.... I am missing something........
    – Riccardo
    Nov 9, 2018 at 15:17
  • Posts (which your $query->set() code addresses) are not the same thing as taxonomy terms (which the $tax_query code addresses). So it's only natural that your two code snippets do different things.
    – Pat J
    Nov 9, 2018 at 15:37

1 Answer 1

1

Main problem with your code is that tax_query should be an array of queries, and not a single query.

$tax_query = array(
    array(
        'taxonomy' => 'filter',
        'terms'    => array(30140, 30020, 30008, 29998, 29991, 21458,20197,11986,6614),
        'field'    => 'term_id',
        'operator' => 'NOT IN'
    )
);
$query->set( 'tax_query', $tax_query );
3
  • Actually I did try: $tax_query = array(); and (with square brackets) $tax_query[] = array( 'taxonomy' => 'filter',..... but it wouldn't work either....wrong usage anyway!
    – Riccardo
    Nov 9, 2018 at 15:49
  • In their example they're using the same IDs for posts and taxonomy terms. I believe their ultimate problem is misunderstanding what tax queries are for, not a syntax issue. Nov 9, 2018 at 16:16
  • @JacobPeattie Yeah, I’ve noticed that. The syntax was one of the problem. The problem with ids is another one... Nov 9, 2018 at 17:58

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.