get_all_meta_children
Description
Returns an array of term_ids with all the children of given term (both by core and by metadata).
Synopsis
<?php get_all_meta_children( $term_id = NULL , $add_parent = true , $field = '_parent' , $depth = -1 , $cur_depth = 0 , $use_cache = true ); ?>
Examples
How to display multiple parentage in a category template
To display all the posts that belong to a category and include multiple parentage (that is, display also the posts that belong to a meta-children term), we'll have to redo a bit the query of The Loop. What we'll do is retrieve all the children of the queried term, and use the list of ids for the category__in
parameter of the query. So, just before The Loop starts (normally it's with if( have_posts() ): ), just add the following:
$posts = query_posts( array( 'category__in' => get_all_meta_children( (int)get_query_var( 'cat' ) ) , 'paged' => get_query_var( 'paged' ) ) );
You can also set some parameters to the get_all_meta_children
function, such as the depth of children to display (that is, the "generations" of children and grandsons &c to include); and, actually, you don't need to pass the query var cat
parameter; it's just there for clarity.
Parameters
- term_id
- add_parent
- field
- depth
- Default: -1 (all levels).
- cur_depth
- Used for recursive control.
- use_cache
- Wheter use the term parentage cache or not. Default: true
Source
<?php function get_all_meta_children( $term_id = NULL , $add_parent = true , $field = '_parent' , $depth = -1 , $cur_depth = 0 , $use_cache = true ){ global $wpdb; $meta_table = $wpdb->prefix . 'termmeta' ; if( !$term_id ) extract( get_term_query_vars() ); if( ( !$cur_depth ) && $add_parent ){ $all_children = array( $term_id ) ; } else { $all_children = array() ; } ; if( $use_cache ){ $children = get_term_meta_children_from_cache( $term_id ); }; if( ( !$use_cache ) || ( !is_array( $children ) ) ){ $query = "SELECT DISTINCT tt.term_id , tt.taxonomy FROM (($wpdb->term_taxonomy AS tt)"; $query .= " LEFT JOIN $meta_table AS tm ON tm.term_id=tt.term_id)" ; $query .= " WHERE (tt.parent=%d) OR ((tm.meta_key=%s) AND (tm.meta_value=%s));" ; $query = $wpdb->prepare( $query , (double)$term_id , (string)$field , (string)$term_id ) ; list( $children , $taxonomies ) = array( array() , array() ); $results = $wpdb->get_results( $query ); foreach( $results as $result ){ $children[] = ( $child_id = (int)$result->term_id ); if( !isset( $taxonomies[$child_id] ) ) $taxonomies[$child_id] = array(); $taxonomies[$child_id][] = $result->taxonomy; }; $children = array_unique( $children ); wp_cache_set( $term_id , $children , 'termmeta_children' ); //Next one is most surely not needed, uncomment it if needed //wp_cache_set( $term_id , $taxonomies , 'termmeta_children_taxonomies' ); }; $all_children = array_merge( $all_children , $children ) ; if( ( $depth < 0 ) || ( $cur_depth < ( $depth ) ) ){ $num_children = count( $children ) ; for( $cur_child = 0 ; $cur_child < $num_children ; $cur_child++ ){ $all_children = array_merge( $all_children , get_all_meta_children( $children[$cur_child] , false , $field , $depth , $cur_depth + 1 , $use_cache ) ) ; } ; } ; if( !$cur_depth ) $all_children = array_map( 'intval' , $all_children ); return $all_children ; } ; ?>