• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / update_object_term_cache() – Updates the cache for the given term object ID(s).

You appear to be a bot. Output may be restricted

Description

Updates the cache for the given term object ID(s).

Note: Due to performance concerns, great care should be taken to only update term caches when necessary. Processing time can increase exponentially depending on both the number of passed term IDs and the number of taxonomies those terms belong to. Caches will only be updated for terms not already cached.

Usage

$void|false = update_object_term_cache( $object_ids, $object_type );

Parameters

$object_ids
( string|int[] ) required – Comma-separated list or array of term object IDs.
$object_type
( string|string[] ) required – The taxonomy object type or array of the same.

Returns

void|false False if all of the terms in $object_ids are already cached.

Source

File name: wordpress/wp-includes/taxonomy.php
Lines:

1 to 62 of 62
function update_object_term_cache( $object_ids, $object_type ) {
  if ( empty( $object_ids ) ) {
    return;
  }

  if ( ! is_array( $object_ids ) ) {
    $object_ids = explode( ',', $object_ids );
  }

  $object_ids     = array_map( 'intval', $object_ids );
  $non_cached_ids = array();

  $taxonomies = get_object_taxonomies( $object_type );

  foreach ( $taxonomies as $taxonomy ) {
    $cache_values = wp_cache_get_multiple( (array) $object_ids, "{$taxonomy}_relationships" );

    foreach ( $cache_values as $id => $value ) {
      if ( false === $value ) {
        $non_cached_ids[] = $id;
      }
    }
  }

  if ( empty( $non_cached_ids ) ) {
    return false;
  }

  $non_cached_ids = array_unique( $non_cached_ids );

  $terms = wp_get_object_terms(
    $non_cached_ids,
    $taxonomies,
    array(
      'fields'                 => 'all_with_object_id',
      'orderby'                => 'name',
      'update_term_meta_cache' => false,
    )
  );

  $object_terms = array();
  foreach ( (array) $terms as $term ) {
    $object_terms[ $term->object_id ][ $term->taxonomy ][] = $term->term_id;
  }

  foreach ( $non_cached_ids as $id ) {
    foreach ( $taxonomies as $taxonomy ) {
      if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) {
        if ( ! isset( $object_terms[ $id ] ) ) {
          $object_terms[ $id ] = array();
        }
        $object_terms[ $id ][ $taxonomy ] = array();
      }
    }
  }

  foreach ( $object_terms as $id => $value ) {
    foreach ( $value as $taxonomy => $terms ) {
      wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
    }
  }
}
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: update_object_term_cache
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/taxonomy.php
File ref: wp-includes/taxonomy.php
Deprecated?: No
API Letters: C,O,T,U

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites