update_post_caches

函数
update_post_caches ( $posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true )
参数
  • (WP_Post[]) $posts Array of post objects (passed by reference).
    Required:
  • (string) $post_type Optional. Post type. Default 'post'.
    Required:
    Default: 'post'
  • (bool) $update_term_cache Optional. Whether to update the term cache. Default true.
    Required:
    Default: true
  • (bool) $update_meta_cache Optional. Whether to update the meta cache. Default true.
    Required:
    Default: true
定义位置
相关方法
update_post_cacheupdate_postmeta_cacheupdate_site_cacheupdate_post_parent_cachesupdate_post_author_caches
引入
1.5.0
弃用
-

update_post_caches: 这个函数更新WordPress数据库中所有文章的缓存。它不接受任何参数。

更新文章、术语和元数据的缓存,为一个文章对象的列表。

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 );
	}
}

常见问题

FAQs
查看更多 >