clean_term_cache()
云策文档标注
概述
clean_term_cache() 函数用于从缓存中移除指定的 term IDs,支持清理单个或整个分类法的缓存。它处理参数并调用相关缓存清理函数,确保数据一致性。
关键要点
- 函数接受三个参数:$ids(必需,单个或数组形式的 term IDs)、$taxonomy(可选,分类法 slug,默认为空)、$clean_taxonomy(可选,布尔值,决定是否清理分类法范围的缓存,默认为 true)。
- 如果 $taxonomy 为空,函数会基于 term IDs 查询数据库获取对应的分类法,并清理 term 对象缓存;否则直接清理指定分类法的缓存。
- 函数内部使用 wp_cache_delete_multiple() 删除 term 缓存,并根据 $clean_taxonomy 参数调用 clean_taxonomy_cache() 清理分类法缓存。
- 触发 clean_term_cache action hook,允许开发者执行自定义操作。
- 函数在缓存失效暂停($_wp_suspend_cache_invalidation)时直接返回,不执行任何操作。
代码示例
function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) {
global $wpdb, $_wp_suspend_cache_invalidation;
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
return;
}
if ( ! is_array( $ids ) ) {
$ids = array( $ids );
}
$taxonomies = array();
if ( empty( $taxonomy ) ) {
$tt_ids = array_map( 'intval', $ids );
$tt_ids = implode( ', ', $tt_ids );
$terms = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" );
$ids = array();
foreach ( (array) $terms as $term ) {
$taxonomies[] = $term->taxonomy;
$ids[] = $term->term_id;
}
wp_cache_delete_multiple( $ids, 'terms' );
$taxonomies = array_unique( $taxonomies );
} else {
wp_cache_delete_multiple( $ids, 'terms' );
$taxonomies = array( $taxonomy );
}
foreach ( $taxonomies as $taxonomy ) {
if ( $clean_taxonomy ) {
clean_taxonomy_cache( $taxonomy );
}
do_action( 'clean_term_cache', $ids, $taxonomy, $clean_taxonomy );
}
wp_cache_set_terms_last_changed();
}注意事项
- 函数自 WordPress 2.3.0 版本引入,是核心缓存管理的一部分,常用于 term 更新或删除操作后保持缓存同步。
- 相关函数包括 wp_cache_delete_multiple()、clean_taxonomy_cache() 和 wp_cache_set_terms_last_changed(),用于辅助缓存清理。
- 在开发插件或主题时,可钩入 clean_term_cache action 以执行自定义缓存逻辑。
原文内容
Removes all of the term IDs from the cache.
Parameters
$idsint|int[]required-
Single or array of term IDs.
$taxonomystringoptional-
Taxonomy slug. Can be empty, in which case the taxonomies of the passed term IDs will be used. Default empty.
$clean_taxonomybooloptional-
Whether to clean taxonomy wide caches (true), or just individual term object caches (false).
Default:
true
Source
function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) {
global $wpdb, $_wp_suspend_cache_invalidation;
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
return;
}
if ( ! is_array( $ids ) ) {
$ids = array( $ids );
}
$taxonomies = array();
// If no taxonomy, assume tt_ids.
if ( empty( $taxonomy ) ) {
$tt_ids = array_map( 'intval', $ids );
$tt_ids = implode( ', ', $tt_ids );
$terms = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" );
$ids = array();
foreach ( (array) $terms as $term ) {
$taxonomies[] = $term->taxonomy;
$ids[] = $term->term_id;
}
wp_cache_delete_multiple( $ids, 'terms' );
$taxonomies = array_unique( $taxonomies );
} else {
wp_cache_delete_multiple( $ids, 'terms' );
$taxonomies = array( $taxonomy );
}
foreach ( $taxonomies as $taxonomy ) {
if ( $clean_taxonomy ) {
clean_taxonomy_cache( $taxonomy );
}
/**
* Fires once after each taxonomy's term cache has been cleaned.
*
* @since 2.5.0
* @since 4.5.0 Added the `$clean_taxonomy` parameter.
*
* @param array $ids An array of term IDs.
* @param string $taxonomy Taxonomy slug.
* @param bool $clean_taxonomy Whether or not to clean taxonomy-wide caches
*/
do_action( 'clean_term_cache', $ids, $taxonomy, $clean_taxonomy );
}
wp_cache_set_terms_last_changed();
}
Hooks
- do_action( ‘clean_term_cache’, array $ids, string $taxonomy, bool $clean_taxonomy )
-
Fires once after each taxonomy’s term cache has been cleaned.
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |