get_object_term_cache

函式
get_object_term_cache ( $id, $taxonomy )
引數
  • (int) $id Term object ID, for example a post, comment, or user ID.
    Required:
  • (string) $taxonomy Taxonomy name.
    Required:
返回值
  • (bool|WP_Term[]|WP_Error) Array of `WP_Term` objects, if cached. False if cache is empty for `$taxonomy` and `$id`. WP_Error if get_term() returns an error object for any term.
定義位置
相關方法
clean_object_term_cacheupdate_object_term_cachewp_get_object_termsget_objects_in_termwp_set_object_terms
引入
2.3.0
棄用
-

get_object_term_cache函式用於檢索特定物件(如文章、評論或使用者)的快取術語資料: 這個函式可以用來避免對術語資料進行不必要的資料庫查詢,以提高效能。

為給定的物件ID檢索快取的術語物件。

上游函式(如get_the_terms()和is_object_in_term())負責填充物件-術語關係緩衝區: 當前的函式只取回已經在快取中的關係資料。

function get_object_term_cache( $id, $taxonomy ) {
	$_term_ids = wp_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_wp_error( $term ) ) {
			return $term;
		}

		$terms[] = $term;
	}

	return $terms;
}

常見問題

FAQs
檢視更多 >