WordPress.org

Make WordPress Core

Opened 14 months ago

Last modified 14 months 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:
PR Number:

Description

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.

<?php
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.

<?php
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 14 months ago.
Bug cause situation

Download all attachments as: .zip

Change History (4)

@mh35
14 months ago

Bug cause situation

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


14 months ago

#2 @joyously
14 months ago

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

'child_of'
(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
14 months ago

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

<?php
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.

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

This returns C1 and C2.

Note: See TracTickets for help on using tickets.