Make WordPress Core

Opened 5 years ago

Last modified 3 months ago

#52904 new defect (bug)

WP_Term_Query does not return deeper descendants of child_of when direct children of child_of are not part of the results

Reported by: xParham Owned by:
Priority: normal Milestone: Awaiting Review
Component: Taxonomy Version:
Severity: major Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Let's say for the hierarchical taxonomy mytax we have the following terms and term ids(in parentheses):

- A (1)
-- AB (2)
--- ABC (3)
- B (4)
-- BD (5)
-- ABC (6)

The following query works fine and it returns both terms with ID 2 and 3

print_r(
    get_terms(
        [
            'taxonomy' => 'mytax',
            'name__like' => 'AB',
            'child_of' => 1,
            'hide_empty' => false
        ]
    )
);

However, the following query returns an empty array (since term_id=2 will no longer be in the results):

print_r(
    get_terms(
        [
            'taxonomy' => 'mytax',
            'name__like' => 'ABC',
            'child_of' => 1,
            'hide_empty' => false
        ]
    )
);

but it is expected that it returns the term ABC with term_id=3.

This seems to be a bug with the logic in _get_term_children() where if direct children of the asking ancestor are not within the initial set of the terms, the recursion will never happen and deeper descendants are then removed from the query results.

Change History (1)

This ticket was mentioned in PR #12177 on WordPress/wordpress-develop by @karunyachavan84.


3 months ago
#1

  • Keywords has-patch has-unit-tests added; needs-patch removed

Trac ticket: https://core.trac.wordpress.org/ticket/52904

## Use of AI Tools

AI assistance: Yes
Tool(s): GitHub Copilot.
Model(s): GPT-4o
Used for: To review and add suitable tests for the modified patch.

---

Note: See TracTickets for help on using tickets.