_prime_term_caches()
云策文档标注
概述
_prime_term_caches() 是一个 WordPress 函数,用于将指定术语 ID 中未缓存的术语添加到缓存中,并可选择更新元数据缓存。
关键要点
- 函数接受两个参数:$term_ids(必需,术语 ID 数组)和 $update_meta_cache(可选,布尔值,默认 true,控制是否更新元数据缓存)。
- 通过 _get_non_cached_ids() 检查未缓存的术语 ID,然后使用 SQL 查询从数据库获取这些术语数据,并调用 update_term_cache() 更新缓存。
- 如果 $update_meta_cache 为 true,会使用 wp_lazyload_term_meta() 延迟加载术语元数据。
- 该函数在 WordPress 6.3.0 中改用 wp_lazyload_term_meta() 进行元数据延迟加载,6.1.0 版本后不再标记为“私有”,最初在 4.6.0 版本引入。
代码示例
function _prime_term_caches( $term_ids, $update_meta_cache = true ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $term_ids, 'terms' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) );
update_term_cache( $fresh_terms );
}
if ( $update_meta_cache ) {
wp_lazyload_term_meta( $term_ids );
}
}注意事项
- 此函数主要用于内部优化,开发者应谨慎直接调用,通常通过相关函数如 WP_Term_Query::get_terms() 间接使用。
- 确保传入的 $term_ids 是有效的整数数组,以避免 SQL 查询错误。
原文内容
Adds any terms from the given IDs to the cache that do not already exist in cache.
Parameters
$term_idsarrayrequired-
Array of term IDs.
$update_meta_cachebooloptional-
Whether to update the meta cache.
Default:
true
Source
function _prime_term_caches( $term_ids, $update_meta_cache = true ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $term_ids, 'terms' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) );
update_term_cache( $fresh_terms );
}
if ( $update_meta_cache ) {
wp_lazyload_term_meta( $term_ids );
}
}
Changelog
| Version | Description |
|---|---|
| 6.3.0 | Use wp_lazyload_term_meta() for lazy-loading of term meta. |
| 6.1.0 | This function is no longer marked as “private”. |
| 4.6.0 | Introduced. |