update_object_term_cache

函数
update_object_term_cache ( $object_ids, $object_type )
参数
  • (string|int[]) $object_ids Comma-separated list or array of term object IDs.
    Required:
  • (string|string[]) $object_type The taxonomy object type or array of the same.
    Required:
返回值
  • (void|false) Void on success or if the `$object_ids` parameter is empty, false if all of the terms in `$object_ids` are already cached.
定义位置
相关方法
get_object_term_cacheupdate_term_cacheclean_object_term_cacheupdate_menu_item_cacheupdate_termmeta_cache
引入
2.3.0
弃用
-

update_object_term_cache: 这个函数更新对象术语的缓存。它需要两个参数:$object_ids和$object_type。$object_ids是一个需要更新术语缓存的对象ID的数组。$object_type是需要更新术语缓存的对象的类型。

更新给定术语对象ID的缓存。

注意:出于对性能的考虑,应该非常注意只在必要时更新术语缓存。处理时间可能会成倍增加,这取决于通过的术语ID的数量和这些术语所属的分类法的数量。

缓存将只对尚未缓存的术语进行更新。

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" );
	}
}

常见问题

FAQs
查看更多 >