update_post_parent_caches()
云策文档标注
概述
update_post_parent_caches() 函数用于更新一组文章对象的父文章缓存,通过提取父文章ID并调用 _prime_post_caches() 来优化数据库查询性能。
关键要点
- 参数 $posts 是必需的 WP_Post 对象数组,用于指定要更新父缓存的文章。
- 函数内部使用 wp_list_pluck() 提取 post_parent 字段,并处理为唯一整数ID数组。
- 仅在父ID数组非空时调用 _prime_post_caches() 来填充缓存,避免不必要的操作。
代码示例
function update_post_parent_caches( $posts ) {
$parent_ids = wp_list_pluck( $posts, 'post_parent' );
$parent_ids = array_map( 'absint', $parent_ids );
$parent_ids = array_unique( array_filter( $parent_ids ) );
if ( ! empty( $parent_ids ) ) {
_prime_post_caches( $parent_ids, false );
}
}注意事项
- 此函数从 WordPress 6.1.0 版本开始引入,适用于需要批量处理文章父级关系的场景。
- 常用于 WP_REST_Posts_Controller、wp_ajax_query_attachments() 和 WP_Media_List_Table 等核心组件中,以提高数据检索效率。
原文内容
Updates parent post caches for a list of post objects.
Parameters
$postsWP_Post[]required-
Array of post objects.
Source
function update_post_parent_caches( $posts ) {
$parent_ids = wp_list_pluck( $posts, 'post_parent' );
$parent_ids = array_map( 'absint', $parent_ids );
$parent_ids = array_unique( array_filter( $parent_ids ) );
if ( ! empty( $parent_ids ) ) {
_prime_post_caches( $parent_ids, false );
}
}
Changelog
| Version | Description |
|---|---|
| 6.1.0 | Introduced. |