Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#29416 closed defect (bug) (invalid)

WP_Query tax_query :: terms array with one item doesn't query properly

Reported by: robertark's profile robertark Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9.2
Component: Query Keywords: reporter-feedback
Focuses: Cc:


Consider this simple query:

$products = new WP_Query(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'orderby' => 'date',
    'order' => 'DESC',
    'meta_query' => array(
        'key' => 'active',
        'compare' => '=',
        'value' => 'Yes'
    'tax_query' => array(
        'taxonomy' => 'product_type',
        'field' => 'slug',
        'terms' => array('accessories'),
        'operator' => 'NOT IN'

When tax_query[terms] is an array with a single element, this tax_query has no effect. When I take 'accessories' out of the array, it works as expected. It would make sense to allow an array as the operator 'NOT IN' basically means 'item NOT IN array', not necessarily 'item NOT IN item'.

Per the documentation:

  • terms can accept: int/string/array

However, it does not list any limitations when accounting for number of items to test against.

Change History (3)

#1 @johnbillion
9 years ago

  • Keywords reporter-feedback added

Thanks for the report Rob.

I'm unable to reproduce the problem. This is the SQL generated (excluding the meta query for brevity):

SELECT wp_posts.*
FROM wp_posts 
WHERE 1=1 
	wp_posts.ID NOT IN ( 
		SELECT object_id 
		FROM wp_term_relationships 
		WHERE term_taxonomy_id IN (3)
AND wp_posts.post_type = 'product'
	wp_posts.post_status = 'publish'
	OR wp_posts.post_status = 'future'
	OR wp_posts.post_status = 'draft'
	OR wp_posts.post_status = 'pending'
	OR wp_posts.post_status = 'private'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC

Some things to check:

  • Does the accessories term actually exist in the product_type taxonomy on your site?
  • Do you have any plugins active on your site which might be filtering the query arguments or the query SQL? You can dump the value of $products->request to see what the SQL looks like.
  • Does the behaviour change if you add more elements to the terms array?

#2 @robertark
9 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Interestingly enough, I tried this again doing both methods, and they both yielded the same request (query). I am certain it was not functioning properly in my initial report along with different tests!

Either way, thank you for following up. I'll be closing the ticket. If it happens again, I'll be sure to test back and forth several times and try to reproduce on a stock WP instance.


#3 @DrewAPicture
9 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.