WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 8 months ago

#14399 closed defect (bug) (duplicate)

get_term_children doesn't call clean_term_cache() if necessary

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

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)

comment:1 miradev4 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()

comment:2 scribu4 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).

comment:3 scribu4 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

comment:4 hakre4 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.

comment:5 miradev4 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).

comment:6 garyc403 years ago

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

comment:7 mdawaffe3 years ago

  • Milestone changed from Awaiting Review to Future Release

comment:8 wonderboymusic8 months 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.