Make WordPress Core

Opened 14 years ago

Closed 11 years ago

#14399 closed defect (bug) (duplicate)

get_term_children doesn't call clean_term_cache() if necessary

Reported by: miradev's profile miradev Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Taxonomy Keywords:
Focuses: Cc:


  1. Create a taxonomy on something 'non standard'
  1. Create a (parent) term.
  1. Create a child term.
  1. Retrieve child terms of the parent term using, amongst others, get_term_children().


register_taxonomy( 'where-you-live', 'user', array('hierarchical' => true,  'show_ui' => true, 'query_var' => true, 'label' => __('Where they live')) );

$country = 'USA';
$state   = 'New York';

$tax_country = wp_insert_term($country,  'where-you-live');
$country_term_id = $tax_country['term_id'];
wp_insert_term($state, 'where-you-live', array('parent'=>$country_term_id));

$state_terms = get_child_terms($country_term_id, 'where-you-live');

I am expecting an array containing the 'New York' child term, but I get nothing.
Have also tried get_terms() with child_of or parent args, which also fail (presumably for the same reason?).

Change History (8)

#1 @miradev
14 years ago

  • Component changed from General to Taxonomy
  • Resolution set to invalid
  • Status changed from new to closed

It is not a bug, it is just that there is a lack of documentation.

get_child_terms() doesn't query the database, it uses the (in this case) 'where-you-live_children' blog option, which is empty until built.

This is built by invoking: clean_term_cache()

#2 @scribu
14 years ago

  • Keywords needs-patch added; custom taxonomy child term removed
  • Resolution invalid deleted
  • Status changed from closed to reopened

You shouldn't need to call clean_term_cache(), so yeah, it's a bug (at least in the docs, as you say).

#3 @scribu
14 years ago

  • Summary changed from get_term_children on non-post custom taxonomies to get_term_children doesn't call clean_term_cache() if necessary

#4 @hakre
14 years ago

Using a function called "clean" to "build" something looks pretty akward - needless to say that from what I read here it might be that the live data structure is just called cache here for no apparent reason.

So please fix in the docs at least.

And btw, why doesn't the code example work? Looks pretty straight forward and valid to me.

Miradev did you refer to some example code in the docs to create your example code? If so, providing more info about that location would be helpful as well.

#5 @miradev
14 years ago

4 weeks languishing here, I thought I was just being a numpty :p
It still doesn't actually work, clean_term_cache() needs to be called somewhere other than in the same init hook that the taxonomy is registered. At best I get it to work by calling it in a subsequent request.

I posted on the forums, trying to get some feedback:

I'll copy the complete example here:

add_action( 'init', 'create_user_location_tax', 0 );
function create_user_location_tax(){
  global $wpdb;
  register_taxonomy( 'location', 'users', array('hierarchical' => true,  'show_ui' => true, 'query_var' => true, 'label' => __('Where are you')) );

  $dummycountry = 'USA';
  $dummystate   = 'KENTUCKY';
  if( taxonomy_exists('location') ){
    $tax_location_terms = get_terms( 'location', 'get=all&hide_empty=false');
    if( count($tax_location_terms) < 1 ){
      $tax_country  = wp_insert_term($dummycountry,  'location');
      $parent       = $tax_country['term_id'];
      $tax_state    = wp_insert_term($dummystate, 'location', array('parent'=>$parent));
      clean_term_cache(array($parent), 'location'); // Doesn't do anything.

  $sql    = "SELECT option_value FROM $wpdb->options WHERE option_name = 'location_children'";
  $result = $wpdb->get_col($sql);
  error_log('term cache: '.$result[0]);

From there, I am a little lost as to what is happening in the WP guts to figure out why it isn't working. My vague feeling was that all the things I tried were hitting cached results (for that particular http request).

#6 @garyc40
13 years ago

The patch for this bug is attached in #14485 along with a detailed description of what the problem is.

#7 @mdawaffe
13 years ago

  • Milestone changed from Awaiting Review to Future Release

#8 @wonderboymusic
11 years ago

  • Keywords needs-patch removed
  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from reopened to closed

#14485 has more chatter and some legacy patches

Note: See TracTickets for help on using tickets.