terms - get_terms not working when child_of is used

admin2025-01-08  6

I have a custom taxonomy registered as department, which works in other queries but it doesn't work when I use:

$args = array( 'child_of' => 483 , 'hide_empty' => false);
$subcats = get_terms( 'department', $args );

foreach( $subcats as $category ) {
    echo $category->name;
}

Once we remove child_of, it prints all the terms. Even using "parent" also not working. What am I missing here? In the database I can check that terms exist as hierarchy.

I have a custom taxonomy registered as department, which works in other queries but it doesn't work when I use:

$args = array( 'child_of' => 483 , 'hide_empty' => false);
$subcats = get_terms( 'department', $args );

foreach( $subcats as $category ) {
    echo $category->name;
}

Once we remove child_of, it prints all the terms. Even using "parent" also not working. What am I missing here? In the database I can check that terms exist as hierarchy.

Share Improve this question edited Sep 29, 2015 at 14:33 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Sep 29, 2015 at 13:27 AlokAlok 11 bronze badge
Add a comment  | 

3 Answers 3

Reset to default 0

I got it solved on my own so, wanted to answer my own question.

in options table the results are cached, so I queried it and delete that.

SELECT * FROM `wp_options` WHERE `option_name` like '%department%'

department is my taxonomy name here.

You can check the function _get_term_hierarchy in taxonomy.php in wp-includes folder.

It's probably cached, but instead of making an SQL query to fix it and potentially break stuff, use the dedicated function instead:

clean_taxonomy_cache( 'department' );

Or if you have WP CLI:

wp cache flush

Other options include:

  • delete_option( 'department_children' );
  • Creating/deleting terms

Use parent Parameter If child_of is not working, they serve similar purposes, but parent is used when you want to retrieve terms that are direct children of a specific parent term.

$args = array( 'parent' => 483 , 'hide_empty' => false);
$subcats = get_terms( 'department', $args );

foreach( $subcats as $category ) {
    echo $category->name;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270430a1428.html

最新回复(0)