get_object_term_cache() WordPress Function

The get_object_term_cache() function is used to retrieve the cached terms for an object. This function is used internally by the get_terms() function, but can also be used directly.

get_object_term_cache( int $id, string $taxonomy ) #

Retrieves the cached term objects for the given object ID.


Description

Upstream functions (like get_the_terms() and is_object_in_term()) are responsible for populating the object-term relationship cache. The current function only fetches relationship data that is already in the cache.


Top ↑

Parameters

$id

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

$taxonomy

(string)(Required)Taxonomy name.


Top ↑

Return

(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.


Top ↑

Source

File: wp-includes/taxonomy.php

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


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Returns a WP_Error object if there's an error with any of the matched terms.
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More