update_post_caches
Call major cache updating functions for list of Post objects.
Description
update_post_caches( (array) &$posts, (string) $post_type = 'post', (constant) $update_term_cache = true, (bool) $update_meta_cache = true );
Parameters (4)
- 0. $posts (array) =>
&$posts
- Array of Post objects
- 1. $post_type — Optional. (string) =>
'post'
- Post type. Default post..
- 2. $update_term_cache — Optional. (constant) =>
true
- Whether to update the term cache. Default true.
- 3. $update_meta_cache — Optional. (bool) =>
true
- Whether to update the meta cache. Default true.
Usage
if ( !function_exists( 'update_post_caches' ) ) { require_once ABSPATH . WPINC . '/post.php'; } // Array of Post objects $posts = array(); // Optional. Post type. Default 'post'. $post_type = 'post'; // Optional. Whether to update the term cache. Default true. $update_term_cache = true; // Optional. Whether to update the meta cache. Default true. $update_meta_cache = true; // NOTICE! Understand what this does before running. $result = update_post_caches($posts, $post_type, $update_term_cache, $update_meta_cache);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/post.php
- function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) {
- // No point in doing all this work if we didn't match any posts.
- if ( !$posts )
- return;
- update_post_cache($posts);
- $post_ids = array();
- foreach ( $posts as $post )
- $post_ids[] = $post->ID;
- if ( ! $post_type )
- $post_type = 'any';
- if ( $update_term_cache ) {
- if ( is_array($post_type) ) {
- $ptypes = $post_type;
- } elseif ( 'any' == $post_type ) {
- $ptypes = array();
- // Just use the post_types in the supplied posts.
- foreach ( $posts as $post ) {
- $ptypes[] = $post->post_type;
- }
- $ptypes = array_unique($ptypes);
- } else {
- $ptypes = array($post_type);
- }
- if ( ! empty($ptypes) )
- update_object_term_cache($post_ids, $ptypes);
- }
- if ( $update_meta_cache )
- update_postmeta_cache($post_ids);
- }