update_object_term_cache()
云策文档标注
概述
update_object_term_cache() 函数用于更新指定对象 ID 的术语缓存,以提高性能。它仅更新未缓存的术语,并返回 void 或 false。
关键要点
- 函数更新给定术语对象 ID 的缓存,参数包括 $object_ids(必需,字符串或整数数组)和 $object_type(必需,字符串或字符串数组)。
- 由于性能考虑,应仅在必要时更新术语缓存,因为处理时间可能随术语 ID 数量和所属分类法数量指数级增加。
- 函数返回 void(成功或 $object_ids 为空时)或 false(所有术语已缓存时)。
- 内部实现涉及检查缓存、获取未缓存 ID、查询术语并更新缓存,使用 wp_cache_get_multiple() 和 wp_cache_add_multiple() 等函数。
代码示例
function update_object_term_cache( $object_ids, $object_type ) {
if ( empty( $object_ids ) ) {
return;
}
if ( ! is_array( $object_ids ) ) {
$object_ids = explode( ',', $object_ids );
}
$object_ids = array_map( 'intval', $object_ids );
$non_cached_ids = array();
$taxonomies = get_object_taxonomies( $object_type );
foreach ( $taxonomies as $taxonomy ) {
$cache_values = wp_cache_get_multiple( (array) $object_ids, "{$taxonomy}_relationships" );
foreach ( $cache_values as $id => $value ) {
if ( false === $value ) {
$non_cached_ids[] = $id;
}
}
}
if ( empty( $non_cached_ids ) ) {
return false;
}
$non_cached_ids = array_unique( $non_cached_ids );
$terms = wp_get_object_terms(
$non_cached_ids,
$taxonomies,
array(
'fields' => 'all_with_object_id',
'orderby' => 'name',
'update_term_meta_cache' => false,
)
);
$object_terms = array();
foreach ( (array) $terms as $term ) {
$object_terms[ $term->object_id ][ $term->taxonomy ][] = $term->term_id;
}
foreach ( $non_cached_ids as $id ) {
foreach ( $taxonomies as $taxonomy ) {
if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) {
if ( ! isset( $object_terms[ $id ] ) ) {
$object_terms[ $id ] = array();
}
$object_terms[ $id ][ $taxonomy ] = array();
}
}
}
$cache_values = array();
foreach ( $object_terms as $id => $value ) {
foreach ( $value as $taxonomy => $terms ) {
$cache_values[ $taxonomy ][ $id ] = $terms;
}
}
foreach ( $cache_values as $taxonomy => $data ) {
wp_cache_add_multiple( $data, "{$taxonomy}_relationships" );
}
}注意事项
使用此函数时需谨慎,避免不必要的调用以防止性能下降。确保参数正确传递,并理解其与相关函数如 wp_get_object_terms() 和 get_object_taxonomies() 的交互。
原文内容
Updates the cache for the given term object ID(s).
Description
Note: Due to performance concerns, great care should be taken to only update term caches when necessary. Processing time can increase exponentially depending on both the number of passed term IDs and the number of taxonomies those terms belong to.
Caches will only be updated for terms not already cached.
Parameters
$object_idsstring|int[]required-
Comma-separated list or array of term object IDs.
$object_typestring|string[]required-
The taxonomy object type or array of the same.
Source
function update_object_term_cache( $object_ids, $object_type ) {
if ( empty( $object_ids ) ) {
return;
}
if ( ! is_array( $object_ids ) ) {
$object_ids = explode( ',', $object_ids );
}
$object_ids = array_map( 'intval', $object_ids );
$non_cached_ids = array();
$taxonomies = get_object_taxonomies( $object_type );
foreach ( $taxonomies as $taxonomy ) {
$cache_values = wp_cache_get_multiple( (array) $object_ids, "{$taxonomy}_relationships" );
foreach ( $cache_values as $id => $value ) {
if ( false === $value ) {
$non_cached_ids[] = $id;
}
}
}
if ( empty( $non_cached_ids ) ) {
return false;
}
$non_cached_ids = array_unique( $non_cached_ids );
$terms = wp_get_object_terms(
$non_cached_ids,
$taxonomies,
array(
'fields' => 'all_with_object_id',
'orderby' => 'name',
'update_term_meta_cache' => false,
)
);
$object_terms = array();
foreach ( (array) $terms as $term ) {
$object_terms[ $term->object_id ][ $term->taxonomy ][] = $term->term_id;
}
foreach ( $non_cached_ids as $id ) {
foreach ( $taxonomies as $taxonomy ) {
if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) {
if ( ! isset( $object_terms[ $id ] ) ) {
$object_terms[ $id ] = array();
}
$object_terms[ $id ][ $taxonomy ] = array();
}
}
}
$cache_values = array();
foreach ( $object_terms as $id => $value ) {
foreach ( $value as $taxonomy => $terms ) {
$cache_values[ $taxonomy ][ $id ] = $terms;
}
}
foreach ( $cache_values as $taxonomy => $data ) {
wp_cache_add_multiple( $data, "{$taxonomy}_relationships" );
}
}
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |