update_object_term_cache() WordPress Function
The update_object_term_cache() function is used to update the cache for an object with the given terms.
update_object_term_cache( string|int[] $object_ids, string|string[] $object_type ) #
Updates the cache for the given term object ID(s).
Description
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.
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.
Return
(void|false) Void on success or if the $object_ids
parameter is empty, false if all of the terms in $object_ids
are already cached.
Source
File: wp-includes/taxonomy.php
3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 | 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 (); } } } $cache_values = array (); foreach ( $object_terms as $id => $value ) { foreach ( $value as $taxonomy => $terms ) { $cache_values [ $taxonomy ][ $id ] = $terms ; } } foreach ( $cache_values as $taxonomy => $data ) { wp_cache_add_multiple( $data , "{$taxonomy}_relationships" ); } } |
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
2.3.0 | Introduced. |