WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get comment meta

Description

get_comment_meta() allows you to pull any meta values stored against comments, which have been set using add_comment_meta(). Similar method to get_post_meta().

Useful within the comments.php template when displaying comments against a post.

Usage

<?php $meta_values get_comment_meta$comment_id$key$single ); ?> 

Parameters

$comment_id
(integer) (required) The ID of the comment from which you want the data. Use $comment->comment_ID to get a comment's ID.
Default: None
$key
(string) (Optional) A string containing the name of the meta value you want. By default, returns data for all keys.
Default: None
$single
(boolean) (optional) If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields. This is not intuitive. For example, if you fetch a serialized array with this method you want $single to be true to actually get an unserialized array back. If you pass in false, or leave it out, you will have an array of one, and the value at index 0 will be the serialized string.
Default: false

Return Value

  • If $single is set to false, or left blank, the function returns an array containing all values of the specified key.
  • If $single is set to true, the function returns the first value of the specified key (not in an array)

The function returns an associative array if the key has not been set and ignores the value of $single.

Example

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

Notes

Change Log

Since: 2.9.0

Source File

get_comment_meta() is located in wp-includes/comment.php

Related

Comment Meta Functions: add_comment_meta(), get_comment_meta(), update_comment_meta(), delete_comment_meta()

See also index of Function Reference and index of Template Tags.