wp_update_comment_count_now()
云策文档标注
概述
wp_update_comment_count_now() 函数用于立即更新指定文章的评论计数。它通过查询数据库获取已批准的评论数量,并更新 posts 表中的 comment_count 字段,同时触发相关 Hook 和缓存清理。
关键要点
- 参数:$post_id(整数,必需),表示要更新评论计数的文章 ID。
- 返回值:成功时返回 true,如果文章不存在则返回 false。
- 核心流程:验证文章 ID,清理相关缓存,获取旧评论计数,通过 pre_wp_update_comment_count_now 过滤器允许自定义新计数,否则从数据库查询,更新 posts 表,清理文章缓存,并触发 wp_update_comment_count 等动作。
- 相关 Hook:包括 pre_wp_update_comment_count_now(过滤新评论计数)、wp_update_comment_count(更新后触发)、edit_post 和 edit_post_{$post_type}(文章更新时触发)。
- 依赖函数:涉及 wp_cache_delete、clean_post_cache、wpdb::update、apply_filters、do_action、get_post、wpdb::get_var 和 wpdb::prepare 等。
代码示例
function wp_update_comment_count_now( $post_id ) {
global $wpdb;
$post_id = (int) $post_id;
if ( ! $post_id ) {
return false;
}
wp_cache_delete( 'comments-0', 'counts' );
wp_cache_delete( "comments-{$post_id}", 'counts' );
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
$old = (int) $post->comment_count;
$new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id );
if ( is_null( $new ) ) {
$new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type != 'note'", $post_id ) );
} else {
$new = (int) $new;
}
$wpdb->update( $wpdb->posts, array( 'comment_count' => $new ), array( 'ID' => $post_id ) );
clean_post_cache( $post );
do_action( 'wp_update_comment_count', $post_id, $new, $old );
do_action( "edit_post_{$post->post_type}", $post_id, $post );
do_action( 'edit_post', $post_id, $post );
return true;
}注意事项
- 函数仅更新单个文章的评论计数,适用于需要即时同步评论数据的场景。
- 在调用前应确保文章 ID 有效,否则会返回 false。
- 通过 pre_wp_update_comment_count_now 过滤器,开发者可以自定义评论计数逻辑,例如排除特定评论类型。
- 更新后会触发多个动作,便于其他插件或主题响应评论计数变化。
- 自 WordPress 2.5.0 版本引入,是核心评论功能的一部分。
原文内容
Updates the comment count for the post.
Parameters
$post_idintrequired-
Post ID
Source
function wp_update_comment_count_now( $post_id ) {
global $wpdb;
$post_id = (int) $post_id;
if ( ! $post_id ) {
return false;
}
wp_cache_delete( 'comments-0', 'counts' );
wp_cache_delete( "comments-{$post_id}", 'counts' );
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
$old = (int) $post->comment_count;
/**
* Filters a post's comment count before it is updated in the database.
*
* @since 4.5.0
*
* @param int|null $new The new comment count. Default null.
* @param int $old The old comment count.
* @param int $post_id Post ID.
*/
$new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id );
if ( is_null( $new ) ) {
$new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type != 'note'", $post_id ) );
} else {
$new = (int) $new;
}
$wpdb->update( $wpdb->posts, array( 'comment_count' => $new ), array( 'ID' => $post_id ) );
clean_post_cache( $post );
/**
* Fires immediately after a post's comment count is updated in the database.
*
* @since 2.3.0
*
* @param int $post_id Post ID.
* @param int $new The new comment count.
* @param int $old The old comment count.
*/
do_action( 'wp_update_comment_count', $post_id, $new, $old );
/** This action is documented in wp-includes/post.php */
do_action( "edit_post_{$post->post_type}", $post_id, $post );
/** This action is documented in wp-includes/post.php */
do_action( 'edit_post', $post_id, $post );
return true;
}
Hooks
- do_action( ‘edit_post’, int $post_id, WP_Post $post )
-
Fires once an existing post has been updated.
- do_action( “edit_post_{$post->post_type}”, int $post_id, WP_Post $post )
-
Fires once an existing post has been updated.
- apply_filters( ‘pre_wp_update_comment_count_now’, int|null $new, int $old, int $post_id )
-
Filters a post’s comment count before it is updated in the database.
- do_action( ‘wp_update_comment_count’, int $post_id, int $new, int $old )
-
Fires immediately after a post’s comment count is updated in the database.
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |