[wp-trac] [WordPress Trac] #27193: tax_query returns only partial results

WordPress Trac noreply at wordpress.org
Mon Oct 20 00:57:55 UTC 2014


#27193: tax_query returns only partial results
-------------------------------------------------+-------------------------
 Reporter:  p_enrique                            |       Owner:
     Type:  defect (bug)                         |      Status:  closed
 Priority:  low                                  |   Milestone:
Component:  Query                                |     Version:  1.5
 Severity:  normal                               |  Resolution:  invalid
 Keywords:  needs-patch needs-unit-tests needs-  |     Focuses:
  docs                                           |
-------------------------------------------------+-------------------------
Changes (by boonebgorges):

 * status:  reopened => closed
 * resolution:   => invalid
 * milestone:  Future Release =>


Comment:

 [28188] introduced a (failing) test for this issue.

 The root cause of this problem is, as suggested by johnbillion
 https://core.trac.wordpress.org/ticket/27193#comment:2, the incorrect use
 of `WP_Query::get_posts()`. The issue is extremely unpleasant to wrap your
 mind around, so if you are lily-livered I recommend you skip the next two
 paragraphs.

 `<awful>`
 When you create a `WP_Query` object using a 'tax_query', a couple of query
 vars are filled in on the WP_Query object for backward compatibility. You
 can read some of the back story starting here
 https://core.trac.wordpress.org/ticket/12659#comment:25 and see the
 original changeset here [16381]. (The current code looks like this
 https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/query.php#L2712 - it's been refactored but it basically does the
 same thing.) What's happening here is this: back in Ye Olden Days of
 WordPress, themers who wanted to build titles for their taxonomy archives
 would do things like `get_query_var( 'cat' )` to figure out what the
 current taxonomy was. Various changes related to the introduction of
 'tax_query' made it the case that the 'cat' query var was not really used
 in most cases anymore, yet when other changes were made that caused 'cat'
 not to be set in some cases, the result was that these kinds of themes
 would have some broken page titles. The fix for backward compatibility was
 to look at the terms that'd been queried by the 'tax_query' argument,
 *grab the first one*, and set it as 'cat' - that way, at least the value
 wouldn't be empty.

 So here's why this is causing the current problem. When you initially
 create the `WP_Query` object, you'll see that the items fetched in the
 `posts` property are correct - the items from cat 1 and cat 2. The
 backward-compatibility code described above sets the `cat` property to '1'
 - recall that this is not used to query anything. Then, when you run
 `get_posts()`, `parse_query()` is run; this method sees `cat=1`, and
 thinks that (for backward compatibility) it should take precedence over
 `tax_query`. So *another* posts query is run, but this one only against
 category 1. Thus the erroneous results you see above.
 `</awful>`

 All the things that p_enrique says here are true
 https://core.trac.wordpress.org/ticket/27193#comment:3. But the fact
 remains that `WP_Query::get_posts()` should only be called internally, and
 really only when setting up the main loop. The documentation should
 probably be updated with a note to this effect.

 Thanks for bearing with us on this one.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/27193#comment:10>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list