On this page
get_object_term_cache( int $id, string $taxonomy ): bool|WP_Term[]|WP_Error
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.
Parameters
$idint Required-
Term object ID, for example a post, comment, or user ID.
$taxonomystring Required-
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.
Source
File: wp-includes/taxonomy.php. View all references
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;
}
Related
Uses
| Uses | Description |
|---|---|
| _prime_term_caches() wp-includes/taxonomy.php | Adds any terms from the given IDs to the cache that do not already exist in cache. |
| wp_cache_get() wp-includes/cache.php | Retrieves the cache contents from the cache by key and group. |
| get_term() wp-includes/taxonomy.php | Gets all term data from database by term ID. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used By
| Used By | Description |
|---|---|
| wp_queue_posts_for_term_meta_lazyload() wp-includes/post.php | Queues posts for lazy-loading of term meta. |
| get_terms_to_edit() wp-admin/includes/taxonomy.php | Gets comma-separated list of terms available to edit for the given post ID. |
| get_inline_data() wp-admin/includes/template.php | Adds hidden fields with the data for use in the inline editor for posts and pages. |
| get_attachment_fields_to_edit() wp-admin/includes/media.php | Retrieves the attachment fields to edit form fields. |
| get_compat_media_markup() wp-admin/includes/media.php | |
| get_the_terms() wp-includes/category-template.php | Retrieves the terms of the taxonomy that are attached to the post. |
| get_the_taxonomies() wp-includes/taxonomy.php | Retrieves all taxonomies associated with a post. |
| is_object_in_term() wp-includes/taxonomy.php | Determines if the given object is associated with any of the given terms. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_object_term_cache