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 | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.0 |
Component: | Taxonomy | Keywords: | |
Focuses: | Cc: |
Description
- Create a taxonomy on something 'non standard'
- Create a (parent) term.
- Create a child term.
- 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
@
14 years ago
- Component changed from General to Taxonomy
- Resolution set to invalid
- Status changed from new to closed
#2
@
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
@
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
@
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
@
14 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).
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()