Make WordPress Core

Opened 16 years ago

Closed 13 years ago

#14399 closed defect (bug) (duplicate)

get_term_children doesn't call clean_term_cache() if necessary

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

Description

  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().

Roughly:

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');
error_log(print_r($state_terms,1));

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
16 years ago

  • Component General → Taxonomy
  • Resolution → invalid
  • Status new → 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
16 years ago

  • Keywords needs-patch added; custom taxonomy child term removed
  • Resolution invalid
  • Status closed → 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
16 years ago

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

#4 @hakre
16 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
16 years ago

@scribu
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.

@hakre
I posted on the forums, trying to get some feedback:
​http://wordpress.org/support/topic/create-hiearchical-taxonomy-terms?replies=1

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
16 years ago

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

#7 @mdawaffe
16 years ago

  • Milestone Awaiting Review → Future Release

#8 @wonderboymusic
13 years ago

  • Keywords needs-patch removed
  • Milestone Future Release
  • Resolution → duplicate
  • Status reopened → closed

#14485 has more chatter and some legacy patches

Note: See TracTickets for help on using tickets.