_prime_post_caches()
Adds the specified posts to the object cache. Posts that are already in the cache are skipped. Also creates a related cache of terms and metadata.
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
1 time — 0.012805 sec (extremely slow) | 50000 times — 0.78 sec (very fast)
No Hooks.
Returns
null. Nothing.
Usage
_prime_post_caches( $ids, $update_term_cache, $update_meta_cache );
- $ids(array) (required)
- Array of post IDs to check and if they are not in the cache, add them to the cache.
- $update_term_cache(true/false)
- Whether to update the related term cache (categories).
Default: true - $update_meta_cache(true/false)
- Whether to update the related metadata cache (custom fields).
Default: true
Examples
#1 Add the posts to the cache when processing comments
An example from the core function get_comments().
// Prime comment post caches.
if ( $this->query_vars['update_comment_post_cache'] ) {
$comment_post_ids = array();
foreach ( $_comments as $_comment ) {
$comment_post_ids[] = $_comment->comment_post_ID;
}
_prime_post_caches( $comment_post_ids, false, false );
} #2 Add the posts to the cache when we get the posts
An example from the core function get_posts().
$ids = $wpdb->get_col( $this->request );
if ( $ids ) {
$this->posts = $ids;
$this->set_found_posts( $q, $limits );
_prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
} else {
$this->posts = array();
}
Notes
- See: update_post_cache()
- See: update_postmeta_cache()
- See: update_object_term_cache()
- Global. wpdb. $wpdb WordPress database abstraction object.
Changelog
| Since 3.4.0 | Introduced. |
| Since 6.1.0 | This function is no longer marked as "private". |