WordPress开发者和主题制作者的搜索引擎



update_object_term_cache ›

Since2.3.0
已弃用n/a
update_object_term_cache ( $object_ids, $object_type )
参数: (2)
  • (string|int[]) $object_ids Comma-separated list or array of term object IDs.
    Required: Yes
  • (string|string[]) $object_type The taxonomy object type or array of the same.
    Required: Yes
返回:
  • (void|false) False if all of the terms in `$object_ids` are already cached.
定义在:
文档:

Updates the cache for the given term object ID(s).

Note: Due to performance concerns, great care should be taken to only update term caches when necessary. Processing time can increase exponentially depending on both the number of passed term IDs and the number of taxonomies those terms belong to.

Caches will only be updated for terms not already cached.



源码

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

	$taxonomies = get_object_taxonomies( $object_type );

	$ids = array();
	foreach ( (array) $object_ids as $id ) {
		foreach ( $taxonomies as $taxonomy ) {
			if ( false === wp_cache_get( $id, "{$taxonomy}_relationships" ) ) {
				$ids[] = $id;
				break;
			}
		}
	}

	if ( empty( $ids ) ) {
		return false;
	}

	$terms = wp_get_object_terms(
		$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 ( $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();
			}
		}
	}

	foreach ( $object_terms as $id => $value ) {
		foreach ( $value as $taxonomy => $terms ) {
			wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
		}
	}
}