WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 8 years ago

#20493 closed defect (bug) (invalid)

WP_Tax_Query->clean_query removes taxonomy term being searched for

Reported by: israelshirk Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: has-patch
Focuses: Cc:

Description

When querying using a tax_query on a hierarchical taxonomy, WP_Tax_Query->clean_query() removes the term ID passed in.

So if I have a taxonomy which is structured like this:

Australia
--Sydney
----East Sydney
----West Sydney
----Whatever else

And objects which are tagged with East, West, and then some are just Sydney.

Now in the parse_query filter we do something like this to include objects tagged as Sydney and its children:

$terms = $state->term_id;

$tax_query[] = Array(
    'taxonomy' => 'location',
    'field' => 'id',
    'terms' => Array($term)
);

$wp_query->set("tax_query", $tax_query);

When clean_query runs through the query, it will remove the term_id for Sydney and replace it with the IDs for East Sydney, West Sydney, and the rest of Sydney's children. This results in any objects only tagged with Sydney not being returned by the query.

Changing line 736 of wp-includes/taxonomy.php from:

$query['terms'] = $children;

to:

$query['terms'] = array_merge($query['terms'], $children);

makes it behave as expected.

Attachments (2)

merge-terms-with-children.diff (495 bytes) - added by wonderboymusic 8 years ago.
20493-ut.diff (2.7 KB) - added by ryan 8 years ago.

Download all attachments as: .zip

Change History (10)

#1 @israelshirk
9 years ago

Or.... I could be in the wrong place entirely :)

#2 @scribu
9 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

Yep, it's a bug. Care to upload the patch?

#3 @wonderboymusic
8 years ago

  • Component changed from General to Taxonomy
  • Keywords has-patch added; needs-patch removed

Patched.

#4 @nacin
8 years ago

  • Milestone changed from Future Release to 3.5

#5 @ryan
8 years ago

  • Keywords needs-unit-tests added

@ryan
8 years ago

#6 @ryan
8 years ago

Those unit tests pass without the patch. That's because clean_query() does this:

$children[] = $term;

The patch doesn't seem necessary.

#8 @scribu
8 years ago

  • Keywords needs-unit-tests removed
  • Milestone 3.5 deleted
  • Resolution set to invalid
  • Status changed from new to closed

Yeah, ryan is right. The only thing the patch does is put the parent terms before the child terms, which makes no difference for the query results.

Note: See TracTickets for help on using tickets.