get_object_term_cache()

最后更新于:2021-11-26 10:26:24

get_object_term_cache( int$id, string$taxonomy)

Retrieves the cached term objects for the given object ID.

参数

$id

(int) (Required) Term object ID, for example a post, comment, or user ID.

$taxonomy

(string) (Required) Taxonomy name.

响应

(bool|GC_Term[]|GC_Error) Array of GC_Term objects, if cached. False if cache is empty for $taxonomy and $id. GC_Error if get_term() returns an error object for any term.

源文件

文件: gc-includes/taxonomy.php

function get_object_term_cache( $id, $taxonomy ) {
	$_term_ids = gc_cache_get( $id, "{$taxonomy}_relationships" );

	// We leave the priming of relationship caches to upstream functions.
	if ( false === $_term_ids ) {
		return false;
	}

	// Backward compatibility for if a plugin is putting objects into the cache, rather than IDs.
	$term_ids = array();
	foreach ( $_term_ids as $term_id ) {
		if ( is_numeric( $term_id ) ) {
			$term_ids[] = (int) $term_id;
		} elseif ( isset( $term_id->term_id ) ) {
			$term_ids[] = (int) $term_id->term_id;
		}
	}

	// Fill the term objects.
	_prime_term_caches( $term_ids );

	$terms = array();
	foreach ( $term_ids as $term_id ) {
		$term = get_term( $term_id, $taxonomy );
		if ( is_gc_error( $term ) ) {
			return $term;
		}

		$terms[] = $term;
	}

	return $terms;
}