get_comment_meta()WP 2.9.0

Gets the metadata of the specified comment.

Comment metadata is identical to post custom fields and is stored in a similar DB table wp_commentmeta. More about metadata.

This function may come in handy for use in the comments.php template file to output additional comment data.

1 time — 0.000503 sec (slow) | 50000 times — 0.12 sec (very fast) | PHP 7.0.2, WP 4.4.2

No Hooks.

Return

Mixed.

  • Returns false when $comment_id parameter passed wrongly.

  • If $single = true

    • string/array — when metafield exists.
    • '' — when metafield not exists.
  • If $single = false
    • array of all metafield values — when metafield exists.
    • array() — when metafield not exists.

Note: If a number is stored in metafield value it will be returned as a string, for example, '54'...

Usage

get_comment_meta( $comment_id, $key, $single );
$comment_id(int) (required)
The ID of the comment whose metadata you want to retrieve.
$key(string)
The meta key to retrieve. By default, returns data for all keys.
Default: ''
$single(true|false)

Whether to return a single value. This parameter has no effect if $key is not specified.

If set to true, the function returns a ready value (string). If false, it returns array() of values (it is needed when there is multiple metafields with same name).

About serialized data - if original data is serialized: if we set 'true' in this parameter we get an array instead of a string. If we leave 'false', we get an array of serialized strings.

Default: false

Examples

#1. Get the value of custom field of the current comment

Get the vote metadata of the current comment:

$vote = get_comment_meta ( $comment->comment_ID, 'vote', true );

echo "Vote: $vote; // Vote: 654

#2. Get the value of the comment meta-field as an array

If we do the same without 'true' in the third argument, we get an array:

$vote = get_comment_meta ( $comment->comment_ID, 'vote' );

// we'll get
Array
(
	[0] => 12
)

Changelog

Since 2.9.0 Introduced.

Code of get_comment_meta() WP 5.9.3

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