Tribe__Cache::warmup_post_caches( array|int $post_ids, bool $update_post_meta_cache = false )
Warms up the caches for a collection of posts.
Contents
Parameters
- $post_ids
-
(array|int) (Required) A post ID, or a collection of post IDs.
- $update_post_meta_cache
-
(bool) (Optional) Whether to warm-up the post meta cache for the posts or not.
Default value: false
Source
File: src/Tribe/Cache.php
public function warmup_post_caches( $post_ids, $update_post_meta_cache = false ) { if ( empty( $post_ids ) ) { return; } $post_ids = (array) $post_ids; global $wpdb; $already_cached_ids = []; foreach ( $post_ids as $post_id ) { if ( wp_cache_get( $post_id, 'posts' ) instanceof \WP_Post ) { $already_cached_ids[] = $post_id; } } $required = array_diff( $post_ids, $already_cached_ids ); if ( empty( $required ) ) { return; } /** @var Tribe__Feature_Detection $feature_detection */ $feature_detection = tribe('feature-detection'); $limit = $feature_detection->mysql_limit_for_example( 'post_result' ); /** * Filters the LIMIT that should be used to warm-up post caches and postmeta caches (if the * `$update_post_meta_cache` parameter is `true`). * * Lower this value on less powerful hosts. Return `0` to disable the warm-up completely, and `-1` to remove the * limit (not recommended). * * @since 4.10.2 * * @param int $limit The number of posts whose caches will be warmed up, per query. */ $limit = (int) apply_filters( 'tribe_cache_warmup_post_cache_limit', min( $limit, count( $post_ids ) ) ); if ( 0 === $limit ) { // Warmup disabled. return; } $buffer = $post_ids; $page = 0; do { $limit_clause = $limit < 0 ? sprintf( 'LIMIT %d,%d', $limit * $page, $limit ) : ''; $page ++; $these_ids = array_splice( $buffer, 0, $limit ); $interval = implode( ',', array_map( 'absint', $these_ids ) ); $posts_query = "SELECT * FROM {$wpdb->posts} WHERE ID IN ({$interval}) {$limit_clause}"; $post_objects = $wpdb->get_results( $posts_query ); if ( is_array( $post_objects ) && ! empty( $post_objects ) ) { foreach ( $post_objects as $post_object ) { $post = new \WP_Post( $post_object ); wp_cache_set( $post_object->ID, $post, 'posts' ); } if ( $update_post_meta_cache ) { update_meta_cache( 'post', $these_ids ); } } } while ( ! empty( $post_objects ) && is_array( $post_objects ) && count( $post_objects ) < count( $post_ids ) ); }
Changelog
Version | Description |
---|---|
4.10.2 | Introduced. |