Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#45457 new defect (bug)

get_terms returns incorrect terms set for indirect children

Reported by: mh35 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.8
Component: Taxonomy Keywords:
Focuses: Cc:


I created categories as below.

未分類(term_id=1) has two children, C1(term_id=2) and C2(term_id=3).
C1 has three children, C3(term_id=4), C4(term_id=5) and C5(term_id=6).
C2 has two children, C6(term_id=7) and C7(term_id=8).

If I query as below, it returns correct answer.

get_terms(array('taxonomy' => 'category', 'hide_empty' => false, 'name' => 'C1', 'child_of' => 1));

This query returns C1 term.

But I query as below, it returns no terms. I think this behavior is incorrect.

get_terms(array('taxonomy' => 'category', 'hide_empty' => false, 'name' => 'C4', 'child_of' => 1));

Attachments (1)

category_structure_cause_bug.png (254.4 KB) - added by mh35 3 years ago.
Bug cause situation

Download all attachments as: .zip

Change History (4)

3 years ago

Bug cause situation

This ticket was mentioned in Slack in #core by mh35. View the logs.

3 years ago

#2 @joyously
3 years ago

As it states in the parameter definitions on https://developer.wordpress.org/reference/classes/wp_term_query/__construct/

(int) Term ID to retrieve child terms of. If multiple taxonomies are passed, $child_of is ignored. Default 0.

So it looks like the correct result to me.

#3 @mh35
3 years ago

If I pass only taxonomy and child_of parameter, this function returns direct and indirect children.

get_terms(array('taxonomy' => 'category', 'hide_empty' => false, 'child_of' => 1));

This returns C1, C3, C4, C5, C2, C6 and C7. If I want to get only direct children, I pass parent parameter.

get_terms(array('taxonomy' => 'category', 'hide_empty' => false, 'parent' => 1));

This returns C1 and C2.

Note: See TracTickets for help on using tickets.