0
votes

I have a custom taxonomy and for each parent there are one or more children, this is an example:

enter image description here

I need to query this taxonomy and get the children of a defined terms.
For example I need to get all the children of MI and in this case should return just Milano.

MI have the ID set to 31 so I used this query to get all the children:

$children = get_term_children(31, 'posizione_geografica'); 

where posizione_geografica is my taxonomy slug.
If var_dump the $children I alway get 0 but, as you can see from the image, MI has children.

How should I modify the query?

1
get_term_children() will return an empty array if the term does not exist in WP. Are you sure 31 is the ID of MI? What page are you trying this on? Can you use get_queried_object() to return the term ID and populate the get_term_children() function dynamically?Ty Bailey
Yes I'm sure about the ID. I have solved with get_termsChristian Giupponi

1 Answers

1
votes

Try this it should work fine

$terms = get_terms( 
        array(
            'taxonomy'   => 'posizione_geografica',
            'hide_empty' => false,
            'parent'     => 31
            ) 
    );