函数文档

update_comment_meta()

💡 云策文档标注

概述

update_comment_meta() 函数用于基于评论 ID 更新评论元数据字段。它通过调用 update_metadata() 实现,支持检查旧值以区分相同键的元数据条目。

关键要点

  • 函数基于评论 ID 更新元数据,若不存在则添加新条目。
  • 使用 $prev_value 参数可指定旧值,仅当匹配时才更新,否则更新所有相同键的条目。
  • 输入时元键和元值需进行“斜杠转义”(slashed),这是历史遗留要求。
  • 返回值:元 ID(如果键不存在)、true(成功更新)、false(失败或值未变)。
  • 相关函数包括 update_metadata() 和 wp_update_comment()。

代码示例

function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) {
    return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
}

📄 原文内容

Updates comment meta field based on comment ID.

Description

Use the $prev_value parameter to differentiate between meta fields with the same key and comment ID.

If the meta field for the comment does not exist, it will be added.

For historical reasons both the meta key and the meta value are expected to be “slashed” (slashes escaped) on input.

Parameters

$comment_idintrequired
Comment ID.
$meta_keystringrequired
Metadata key.
$meta_valuemixedrequired
Metadata value. Must be serializable if non-scalar.
$prev_valuemixedoptional
Previous value to check before updating.
If specified, only update existing metadata entries with this value. Otherwise, update all entries. Default empty string.

Return

int|bool Meta ID if the key didn’t exist, true on successful update, false on failure or if the value passed to the function is the same as the one that is already in the database.

Source

function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) {
	return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
}

Changelog

Version Description
2.9.0 Introduced.

User Contributed Notes