函数文档

update_posts_count()

💡 云策文档标注

概述

update_posts_count() 函数用于更新博客的文章计数,以避免在获取博客详情时进行额外的数据库查询。它通常在文章发布或取消发布时被调用,以确保计数保持最新。

关键要点

  • 函数更新 WordPress 多站点中存储为选项的博客文章计数,优化性能。
  • 参数 $deprecated 已弃用,无需使用。
  • 通过 update_option() 和 wpdb::get_var() 实现计数更新。

代码示例

function update_posts_count( $deprecated = '' ) {
	global $wpdb;
	update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ), true );
}

注意事项

  • 此函数自 MU (3.0.0) 版本引入,适用于多站点环境。
  • 相关函数包括 _update_posts_count_on_delete() 和 _update_posts_count_on_transition_post_status(),用于处理文章删除或状态变更时的计数更新。

📄 原文内容

Updates a blog’s post count.

Description

WordPress MS stores a blog’s post count as an option so as to avoid extraneous COUNTs when a blog’s details are fetched with get_site() . This function is called when posts are published or unpublished to make sure the count stays current.

Parameters

$deprecatedstringrequired
Not used.

Source

function update_posts_count( $deprecated = '' ) {
	global $wpdb;
	update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ), true );
}

Changelog

Version Description
MU (3.0.0) Introduced.