<?php function init_term_meta_cache(){
global $wpdb;
$term_meta_cache = array();
$table = $wpdb->prefix . "termmeta" ;
$query = $wpdb->prepare( "SELECT * FROM $table ORDER BY tmeta_id" ) ;
$metas = $wpdb->get_results( $query );
list( $used_term_ids , $term_id_list ) = array( array() , array() );
foreach( $metas as $meta ){
list( $term_id , $meta_key , $meta_value ) = array(
(int)$meta->term_id , $meta->meta_key , $meta->meta_value );
if( !$used_term_ids[$term_id] ){
$used_term_ids[$term_id] = true;
$term_id_list[] = $term_id;
};
if( !isset( $term_meta_cache[$term_id] ) ) $term_meta_cache[$term_id] = array();
$term_meta_cache[$term_id][] = (object)array( 'meta_key' => $meta_key , 'meta_value' => $meta_value );
// Add to term group cache
if( 'term_group' == $meta_key ){
if( !( $term_group_cache = wp_cache_get( $meta_value , 'term_group_ids' ) ) )
$term_group_cache = array();
$term_group_cache[] = $term_id;
wp_cache_set( $meta_value , $term_group_cache , 'term_group_ids' );
};
//and fill to termmeta_parent and termmeta_children cache
if( '_parent' == $meta_key ){
if( !( $cached_children = wp_cache_get( (int)$meta_value , 'termmeta_children' ) ) )
$cached_children = array();
$cached_children[] = $term_id;
wp_cache_set( (int)$meta_value , $cached_children , 'termmeta_children' );
if( !( $cached_parents = wp_cache_get( $term_id , 'termmeta_parents' ) ) )
$cached_parents = array();
$cached_parents[] = (int)$meta_value;
wp_cache_set( $term_id , $cached_parents , 'termmeta_parents' );
};
};
foreach( $term_id_list as $term_id ){
wp_cache_set( $term_id , $term_meta_cache[$term_id] , 'termmeta' );
};
};
// ToDo: Make the following an option
add_action( 'plugins_loaded' , 'init_term_meta_cache' , 1 ); ?>
No Comments