_prime_post_caches( int[] $ids, bool $update_term_cache = true, bool $update_meta_cache = true )

Adds any posts from the given IDs to the cache that do not already exist in cache.

Description

See also

Parameters

$idsint[]required
ID list.
$update_term_cachebooloptional
Whether to update the term cache.

Default:true

$update_meta_cachebooloptional
Whether to update the meta cache.

Default:true

Source

function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) {
	global $wpdb;

	$non_cached_ids = _get_non_cached_ids( $ids, 'posts' );
	if ( ! empty( $non_cached_ids ) ) {
		$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );

		if ( $fresh_posts ) {
			// Despite the name, update_post_cache() expects an array rather than a single post.
			update_post_cache( $fresh_posts );
		}
	}

	if ( $update_meta_cache ) {
		update_postmeta_cache( $ids );
	}

	if ( $update_term_cache ) {
		$post_types = array_map( 'get_post_type', $ids );
		$post_types = array_unique( $post_types );
		update_object_term_cache( $ids, $post_types );
	}
}

Changelog

VersionDescription
6.1.0This function is no longer marked as "private".
3.4.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.