WordPress设置评论的状态函数:wp_set_comment_status

AI大语言模型

WordPress函数wp_set_comment_status()用于设置评论的状态,允许为指定评论ID或WP_Comment对象的评论更新状态。状态可以是hold、approve、spam或trash。

wp_set_comment_status( int|WP_Comment $comment_id,  string $comment_status,  bool $wp_error = false ): bool|WP_Error

函数参数

$comment_id

数据类型:int|WP_Comment (必须)

评论ID或WP_Comment对象。

$comment_status

数据类型:string (必须)

新的评论状态,可以是hold、approve、spam或trash。

$wp_error

数据类型:bool (可选)

如果发生异常,是否返回WP_Error对象。

函数返回值

bool | WP_Error

成功时为True,失败时为false或WP_Error。

函数使用示例

将ID为123的评论状态更改为批准:

$comment_id = 123;
$new_status = 'approve';
$result = wp_set_comment_status($comment_id, $new_status);

if ($result) {
    echo '评论已成功批准。';
} else {
    echo '批准评论时出错。';
}

将所有标记为垃圾的评论移至回收站:

$comments = get_comments(array('status' => 'spam'));

foreach ($comments as $comment) {
    wp_set_comment_status($comment->comment_ID, 'trash');
    echo '评论ID为 ' . $comment->comment_ID . ' 已被移至回收站。';
}

扩展阅读

wp_set_comment_status()函数位于:wp-includes/comment.php

相关函数:

  • wp_update_comment_count()
  • wp_transition_comment_status()
  • get_comment()
  • wp_spam_comment()
  • wp_unspam_comment()
  • wp_trash_comment()
  • wp_untrash_comment()
AI大语言模型