get_object_term_cache

The timeline below displays how wordpress function get_object_term_cache has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.6

/**
 * Retrieves the cached term objects for the given object ID.
 *
 * 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.
 *
 * @since 2.3.0
 * @since 4.7.0 Returns a `WP_Error` object if there's an error with
 *              any of the matched terms.
 *
 * @param int    $id       Term object ID, for example a post, comment, or user ID.
 * @param string $taxonomy Taxonomy name.
 * @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.
 */
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;
}

WordPress Version: 5.3

/**
 * Retrieves the cached term objects for the given object ID.
 *
 * 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.
 *
 * @since 2.3.0
 * @since 4.7.0 Returns a `WP_Error` object if there's an error with
 *              any of the matched terms.
 *
 * @param int    $id       Term object ID, for example a post, comment, or user ID.
 * @param string $taxonomy Taxonomy name.
 * @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.
 */
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[] = intval($term_id);
        } elseif (isset($term_id->term_id)) {
            $term_ids[] = intval($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;
}

WordPress Version: 5.1

/**
 * Retrieves the taxonomy relationship to the term object id.
 *
 * 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.
 *
 * @since 2.3.0
 * @since 4.7.0 Returns a `WP_Error` object if `get_term()` returns an error for
 *              any of the matched terms.
 *
 * @param int    $id       Term object ID.
 * @param string $taxonomy Taxonomy name.
 * @return bool|array|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.
 */
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[] = intval($term_id);
        } elseif (isset($term_id->term_id)) {
            $term_ids[] = intval($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;
}

WordPress Version: 4.7

/**
 * Retrieves the taxonomy relationship to the term object id.
 *
 * 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.
 *
 * @since 2.3.0
 * @since 4.7.0 Returns a WP_Error object if get_term() returns an error for
 *              any of the matched terms.
 *
 * @param int    $id       Term object ID.
 * @param string $taxonomy Taxonomy name.
 * @return bool|array|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.
 */
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[] = intval($term_id);
        } elseif (isset($term_id->term_id)) {
            $term_ids[] = intval($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;
}

WordPress Version: 4.6

/**
 * Retrieves the taxonomy relationship to the term object id.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param int    $id       Term object ID.
 * @param string $taxonomy Taxonomy name.
 * @return bool|array Array of `WP_Term` objects, if cached False if cache is empty for `$taxonomy` and `$id`.
 */
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[] = intval($term_id);
        } elseif (isset($term_id->term_id)) {
            $term_ids[] = intval($term_id->term_id);
        }
    }
    // Fill the term objects.
    _prime_term_caches($term_ids);
    $terms = array();
    foreach ($term_ids as $term_id) {
        $terms[] = wp_cache_get($term_id, 'terms');
    }
    return array_map('get_term', $terms);
}

WordPress Version: 4.3

/**
 * Retrieves the taxonomy relationship to the term object id.
 *
 * @since 2.3.0
 *
 * @param int    $id       Term object ID.
 * @param string $taxonomy Taxonomy name.
 * @return bool|mixed Empty array if $terms found, but not `$taxonomy`. False if nothing is in cache
 *                    for `$taxonomy` and `$id`.
 */
function get_object_term_cache($id, $taxonomy)
{
    return wp_cache_get($id, "{$taxonomy}_relationships");
}

WordPress Version: 4.2

/**
 * Retrieves the taxonomy relationship to the term object id.
 *
 * @since 2.3.0
 *
 * @param int    $id       Term object ID
 * @param string $taxonomy Taxonomy Name
 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
 */
function get_object_term_cache($id, $taxonomy)
{
    $cache = wp_cache_get($id, "{$taxonomy}_relationships");
    return $cache;
}

WordPress Version: 4.1

/**
 * Retrieves the taxonomy relationship to the term object id.
 *
 * @since 2.3.0
 *
 * @param int|array $id Term object ID
 * @param string $taxonomy Taxonomy Name
 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
 */
function get_object_term_cache($id, $taxonomy)
{
    $cache = wp_cache_get($id, "{$taxonomy}_relationships");
    return $cache;
}

WordPress Version: 3.9

/**
 * Retrieves the taxonomy relationship to the term object id.
 *
 * @since 2.3.0
 *
 * @uses wp_cache_get() Retrieves taxonomy relationship from cache
 *
 * @param int|array $id Term object ID
 * @param string $taxonomy Taxonomy Name
 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
 */
function get_object_term_cache($id, $taxonomy)
{
    $cache = wp_cache_get($id, "{$taxonomy}_relationships");
    return $cache;
}

WordPress Version: 3.7

/**
 * Retrieves the taxonomy relationship to the term object id.
 *
 * @package WordPress
 * @subpackage Taxonomy
 * @since 2.3.0
 *
 * @uses wp_cache_get() Retrieves taxonomy relationship from cache
 *
 * @param int|array $id Term object ID
 * @param string $taxonomy Taxonomy Name
 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
 */
function get_object_term_cache($id, $taxonomy)
{
    $cache = wp_cache_get($id, "{$taxonomy}_relationships");
    return $cache;
}