函数文档

update_term_cache()

💡 云策文档标注

概述

update_term_cache() 函数用于更新缓存中的分类法术语对象,通过克隆术语对象并移除 object_id 属性后,批量添加到缓存中。

关键要点

  • 函数接受两个参数:$terms(必需,WP_Term 对象数组)和 $taxonomy(必需,字符串,但未使用)。
  • 内部处理:遍历术语数组,克隆每个术语对象以避免引用问题,并清除 object_id 属性以防止缓存不必要数据。
  • 使用 wp_cache_add_multiple() 将处理后的术语对象批量添加到缓存中,键为 term_id。
  • 该函数自 WordPress 2.3.0 版本引入,主要用于优化术语缓存性能。

相关函数

  • wp_cache_add_multiple():用于一次性添加多个值到缓存。
  • prime_term_caches():用于将指定 ID 的术语添加到缓存中(如果尚未缓存)。

📄 原文内容

Updates terms in cache.

Parameters

$termsWP_Term[]required
Array of term objects to change.
$taxonomystringrequired
Not used.

Source

function update_term_cache( $terms, $taxonomy = '' ) {
	$data = array();
	foreach ( (array) $terms as $term ) {
		// Create a copy in case the array was passed by reference.
		$_term = clone $term;

		// Object ID should not be cached.
		unset( $_term->object_id );

		$data[ $term->term_id ] = $_term;
	}
	wp_cache_add_multiple( $data, 'terms' );
}

Changelog

Version Description
2.3.0 Introduced.