#29031 closed defect (bug) (worksforme)
Wp Query multiple tax_query results in unexpected behavior under certain conditions
Reported by: | dunar21 | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | trivial | Version: | 3.9.1 |
Component: | Query | Keywords: | |
Focuses: | Cc: |
Description
Considering this:
$args = array( 'post_type' => 'post', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'movie_genre', 'field' => 'slug', 'terms' => array('action') ), array( 'taxonomy' => 'actors', 'field' => 'id', 'terms' => array(25, 26), 'operator' => 'IN' ) ) ); $query = new WP_Query( $args );
I would expect the result set to include all posts that are assigned a 'movie_genre' taxonomy of 'action' AND all of those movies, of that genre, who have actors (tax) with id's of 25 and 26.
This is the expected result, however, if actor 25 has no posts associated with his tax term id and actor 26 does, the result set will return empty.
If the terms are switched :
$args = array( 'post_type' => 'post', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'movie_genre', 'field' => 'slug', 'terms' => array('action') ), array( 'taxonomy' => 'actors', 'field' => 'id', 'terms' => array(26, 25),/*!!!!! CHANGE HERE !!!!*/ 'operator' => 'IN' ) ) ); $query = new WP_Query( $args );
Then the result set turns out as expected. All posts of genre action with actor 26 are displayed (because actor 25 has no posts). So, because the first in the terms array for the second tax_query has results, it queries correctly. If reversed, because the first in the terms array for the second tax_query has no results, it queries incorrectly.
I believe this behavior is a bug. The "IN" operator should return a result set similar to "OR" despite the order of placement of term ids for the second tax_query.
I have not tested if the same occurs for the first query, or for one individual tax_query. However, the expected action of this type of query would be :
"Give me all POSTS which have a GENRE of ACTION and include any of the ACTORS in this ARRAY(IDS).
Attachments (1)
Change History (5)
#1
@
10 years ago
- Summary changed from Wp Query tax_query multiple query individual OR query to Wp Query multiple tax_query results in unexpected behavior under certain conditions
#3
@
10 years ago
- Resolution set to worksforme
- Severity changed from normal to trivial
- Status changed from new to closed
I just tested the query and it works perfectly well now. Maybe it was an issue with the older version of WP. If you were testing against 3.9.1 and had no issues, then maybe it was an error on my part, however, I went through the code meticulously in order to solve the problem. Still, I make mistakes all the time and it is very possible. Thanks for looking into it for me. Again, testing against 4.0, I cannot replicate the error myself and I no longer have the original code base I was working on when I encountered this issue. This topic can be marked closed. Thanks again.
dunar21 - Thanks for the report, and sorry for the delay in getting around to looking at it.
I've attempted to reproduce your issue in a unit test, but I've been unsuccessful. In 29031.test.patch, I've tried to match your query, but the tests are passing as you'd expect.
Looking at the code that parses 'tax_query', I'm at somewhat of a loss to understand how the order of the 'terms' argument could matter. Your description of the problem suggests that the fact that the 25 actor doesn't have posts is causing the tax query to short circuit, but I don't see how this could happen - the two terms are processed together, in a SQL
IN
clause, not serially. SeeWP_Tax_Query::transform_query()
.Can you share the SQL clauses that are being generated by
WP_Tax_Query::get_sql()
?