函数文档

get_comment_meta()

💡 云策文档标注

概述

get_comment_meta() 函数用于检索评论的元数据字段,基于评论 ID 和可选参数返回单个值或数组。它是 get_metadata() 的封装,专门处理评论类型的元数据。

关键要点

  • 参数:$comment_id(必需,评论 ID),$key(可选,元键,默认为空字符串返回所有键),$single(可选,是否返回单个值,默认为 false)。
  • 返回值:根据 $single 参数返回数组或单个值;无效 $comment_id 返回 false;非存在评论 ID 根据 $single 返回空数组或空字符串。
  • 注意:非序列化值以字符串形式返回(如 false 返回空字符串,true 返回 '1'),数组和对象保持原始类型。

代码示例

// Return a single meta value with the key 'vote' from a defined comment object.
$vote = get_comment_meta( $comment->comment_ID, 'vote', true );

📄 原文内容

Retrieves comment meta field for a comment.

Parameters

$comment_idintrequired
Comment ID.
$keystringoptional
The meta key to retrieve. By default, returns data for all keys. Default empty string.
$singlebooloptional
Whether to return a single value.
This parameter has no effect if $key is not specified.

Default:false

Return

mixed An array of values if $single is false.
The value of meta data field if $single is true.
False for an invalid $comment_id (non-numeric, zero, or negative value).
An empty array if a valid but non-existing comment ID is passed and $single is false.
An empty string if a valid but non-existing comment ID is passed and $single is true.
Note: Non-serialized values are returned as strings:

  • false values are returned as empty strings ('')
  • true values are returned as '1'
  • numbers are returned as strings Arrays and objects retain their original type.

Source

function get_comment_meta( $comment_id, $key = '', $single = false ) {
	return get_metadata( 'comment', $comment_id, $key, $single );
}

Changelog

Version Description
2.9.0 Introduced.

User Contributed Notes