clean_object_term_cache()
云策文档标注
概述
clean_object_term_cache() 函数用于从缓存中移除对象与分类法术语之间的关系。它通过删除指定对象ID和对象类型相关的缓存条目来清理术语缓存。
关键要点
- 函数接受两个必需参数:$object_ids(单个或数组形式的术语对象ID)和 $object_type(分类法对象类型)。
- 内部使用 wp_cache_delete_multiple() 删除多个缓存值,并调用 wp_cache_set_terms_last_changed() 更新缓存最后更改时间。
- 函数在清理后触发 'clean_object_term_cache' 钩子,允许开发者执行自定义操作。
- 如果全局变量 $_wp_suspend_cache_invalidation 非空,函数会直接返回而不执行缓存清理。
代码示例
function clean_object_term_cache( $object_ids, $object_type ) {
global $_wp_suspend_cache_invalidation;
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
return;
}
if ( ! is_array( $object_ids ) ) {
$object_ids = array( $object_ids );
}
$taxonomies = get_object_taxonomies( $object_type );
foreach ( $taxonomies as $taxonomy ) {
wp_cache_delete_multiple( $object_ids, "{$taxonomy}_relationships" );
}
wp_cache_set_terms_last_changed();
do_action( 'clean_object_term_cache', $object_ids, $object_type );
}注意事项
- 确保 $object_ids 和 $object_type 参数正确,否则可能无法有效清理缓存。
- 函数依赖于 get_object_taxonomies() 来获取相关分类法,需确保对象类型已注册。
- 在缓存失效被挂起时(通过 $_wp_suspend_cache_invalidation),函数不会执行任何操作。
原文内容
Removes the taxonomy relationship to terms from the cache.
Description
Will remove the entire taxonomy relationship containing term $object_id. The term IDs have to exist within the taxonomy $object_type for the deletion to take place.
See also
- get_object_taxonomies(): for more on $object_type.
Parameters
$object_idsint|arrayrequired-
Single or list of term object ID(s).
$object_typearray|stringrequired-
The taxonomy object type.
Source
function clean_object_term_cache( $object_ids, $object_type ) {
global $_wp_suspend_cache_invalidation;
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
return;
}
if ( ! is_array( $object_ids ) ) {
$object_ids = array( $object_ids );
}
$taxonomies = get_object_taxonomies( $object_type );
foreach ( $taxonomies as $taxonomy ) {
wp_cache_delete_multiple( $object_ids, "{$taxonomy}_relationships" );
}
wp_cache_set_terms_last_changed();
/**
* Fires after the object term cache has been cleaned.
*
* @since 2.5.0
*
* @param array $object_ids An array of object IDs.
* @param string $object_type Object type.
*/
do_action( 'clean_object_term_cache', $object_ids, $object_type );
}
Hooks
- do_action( ‘clean_object_term_cache’, array $object_ids, string $object_type )
-
Fires after the object term cache has been cleaned.
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |