Make WordPress Core

Opened 12 years ago

Closed 11 years ago

#29185 closed defect (bug) (fixed)

get_terms with parent set incorrectly hides non-empty terms

Reported by: smerriman's profile smerriman Owned by: boonebgorges's profile boonebgorges
Milestone: 4.1 Priority: normal
Severity: normal Version: 3.9.1
Component: Taxonomy Keywords:
Focuses: Cc:

Description

In #26903, it was reported that there was a bug when get_terms is called with hierarchical=true and parent=0 - it would automatically reset hierarchical to false, and therefore hide parent terms which contained non-empty children.

This was fixed by hardcoding a check for parent=0.

However, this should apply for every other value of parent as well. For example, let's say I had:

  • One (0)
    • Two (0)
      • Three (2)
      • Four (3)
    • Five (0)
      • Six (2)

Calling get_terms with a parent set to category 'One' will return no terms, while it should.

Attachments (3)

29185.patch (2.8 KB) - added by landakram 11 years ago.
29185.2.patch (1.3 KB) - added by boonebgorges 11 years ago.
29185.3.patch (7.2 KB) - added by landakram 11 years ago.

Download all attachments as: .zip

Change History (9)

#1 @boonebgorges
11 years ago

  • Keywords needs-unit-tests good-first-bug added

@landakram
11 years ago

#2 @landakram
11 years ago

If I understand correctly, then for any value of parent, hierarchical should not be automatically reset, since the cases that were already being excluded were when parent == '' and when parent == 0.

I attached a patch which removes the value checks for parent there entirely. I also added a test for the setup described above, which verifies that the patch has the intended behavior.

Apologies for uploading the patch with the wrong extension — I'm not sure how to remove it!

Last edited 11 years ago by landakram (previous) (diff)

#3 @boonebgorges
11 years ago

  • Keywords good-first-bug removed

landakram and I worked on this for a while at WCSF contributor day. First off, I am a bozo for marking it good-first-bug. Second, I think we have the proper fix in 29185.2.patch, but we need to have some more unit tests that describe the current behavior of 'child_of', 'parent', and 'hierarchical', especially in cases where one overrides another.

@landakram
11 years ago

#4 @landakram
11 years ago

I wrote some additional unit tests that describe the interplay between child_of, parent, and hierarchical. I used this hierarchy for the tests:

  • One
    • Two
      • Three (1)
      • Four
    • Five
      • Six (1)
    • Seven

Here's what I found:

  1. When hierarchical == true and child_of is set, both children and non-empty descendants are returned. In other words, in the hierarchy described, Two, Three, Five, and Six are returned.
  2. When hierarchical == false and child_of is set, only direct children are returned. In the above hierarchy, the returned array is empty. I wrote another test which the hierarchy so that a post was associated with Seven, and the returned array contained only Seven.
  3. When both child_of and parent are set, parent takes precedent over child_of.
  1. When hierarchical == true and parent is set, the returned array is empty. This is the bug — it should return Two and Five.
  2. When hierarchical == false and parent is set, the returned array is empty.
  3. When hierarchical == true and both child_of and parent are set, the returned array is empty. This is the bug — to be consistent with the behavior of 3, it should return Two and Five.
  4. When hierarchical == false and both child_of and parent are set, the returned array is empty.

The attached patch includes the unit tests and the fix that boonebgorges and I came up with. When patched, 4 and 5 are fixed (test_hierarchical_parent_get_terms and test_hierarchical_parent_child_of_get_terms respectively).

Now, I can't say whether the existing behaviors make 'sense', but the proposed patch leaves those behaviors intact while fixing this bug :)

Last edited 11 years ago by landakram (previous) (diff)

#5 @boonebgorges
11 years ago

  • Keywords needs-unit-tests removed
  • Milestone changed from Awaiting Review to 4.1
  • Owner set to boonebgorges
  • Status changed from new to accepted

Thanks for the additional tests, landakram. The behavior of these parameters is pretty confusing, and it's extremely valuable to have these tests to demonstrate a bit more clearly how they work together.

#6 @jeremyfelt
11 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

Fixed in [30107], commit missed the ticket.

Note: See TracTickets for help on using tickets.