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

  1. if ( !function_exists( 'update_post_caches' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Array of Post objects 
  5. $posts = array(); 
  6.  
  7. // Optional. Post type. Default 'post'. 
  8. $post_type = 'post'; 
  9.  
  10. // Optional. Whether to update the term cache. Default true. 
  11. $update_term_cache = true; 
  12.  
  13. // Optional. Whether to update the meta cache. Default true. 
  14. $update_meta_cache = true; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = update_post_caches($posts, $post_type, $update_term_cache, $update_meta_cache); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { 
  2. // No point in doing all this work if we didn't match any posts. 
  3. if ( !$posts ) 
  4. return; 
  5.  
  6. update_post_cache($posts); 
  7.  
  8. $post_ids = array(); 
  9. foreach ( $posts as $post ) 
  10. $post_ids[] = $post->ID; 
  11.  
  12. if ( ! $post_type ) 
  13. $post_type = 'any'; 
  14.  
  15. if ( $update_term_cache ) { 
  16. if ( is_array($post_type) ) { 
  17. $ptypes = $post_type; 
  18. } elseif ( 'any' == $post_type ) { 
  19. $ptypes = array(); 
  20. // Just use the post_types in the supplied posts. 
  21. foreach ( $posts as $post ) { 
  22. $ptypes[] = $post->post_type; 
  23. $ptypes = array_unique($ptypes); 
  24. } else { 
  25. $ptypes = array($post_type); 
  26.  
  27. if ( ! empty($ptypes) ) 
  28. update_object_term_cache($post_ids, $ptypes); 
  29.  
  30. if ( $update_meta_cache ) 
  31. update_postmeta_cache($post_ids);