函数文档

wp_get_comment_status()

💡 云策文档标注

概述

wp_get_comment_status() 函数用于根据评论 ID 或 WP_Comment 对象检索评论的状态。它返回一个字符串表示状态,如 'approved'、'unapproved'、'spam' 或 'trash',失败时返回 false。

关键要点

  • 参数:$comment_id(int 或 WP_Comment 对象),必需,指定评论 ID 或对象
  • 返回值:字符串(状态值)或 false(失败时)
  • 状态值可能包括 'trash'、'approved'、'unapproved'、'spam'
  • 内部实现基于 comment_approved 字段进行映射

代码示例

$status = wp_get_comment_status( $comment_id );
if ( 'approved' === $status ) {
  // Show the comment.
}

📄 原文内容

Retrieves the status of a comment by comment ID.

Parameters

$comment_idint|WP_Commentrequired
Comment ID or WP_Comment object

Return

string|false Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.

Source

function wp_get_comment_status( $comment_id ) {
	$comment = get_comment( $comment_id );
	if ( ! $comment ) {
		return false;
	}

	$approved = $comment->comment_approved;

	if ( null === $approved ) {
		return false;
	} elseif ( '1' === $approved ) {
		return 'approved';
	} elseif ( '0' === $approved ) {
		return 'unapproved';
	} elseif ( 'spam' === $approved ) {
		return 'spam';
	} elseif ( 'trash' === $approved ) {
		return 'trash';
	} else {
		return false;
	}
}

Changelog

Version Description
1.0.0 Introduced.

User Contributed Notes