Using WordPress ‘get_comment()’ PHP function

The get_comment() WordPress PHP function retrieves comment data given a comment ID or comment object.

Usage

get_comment( $comment, $output )

Custom Example:

$comment_id = 7;
$comment_data = get_comment($comment_id);
echo $comment_data->comment_author;

Parameters

  • $comment (WP_Comment|string|int, Optional) – Comment to retrieve. Default: null
  • $output (string, Optional) – The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Comment object, an associative array, or a numeric array, respectively. Default: OBJECT

More information

See WordPress Developer Resources: get_comment()

Examples

Get comment data as an object

Retrieve the comment data as a WP_Comment object.

$comment_id = 10;
$comment_data = get_comment($comment_id);
echo $comment_data->comment_author;

Get comment data as an associative array

Retrieve the comment data as an associative array.

$comment_id = 10;
$comment_data = get_comment($comment_id, ARRAY_A);
echo $comment_data['comment_author'];

Get comment data as a numeric array

Retrieve the comment data as a numeric array.

$comment_id = 10;
$comment_data = get_comment($comment_id, ARRAY_N);
echo $comment_data[0]; // comment_ID

Get comment author’s email

Retrieve the email of the comment author.

$comment_id = 15;
$comment_data = get_comment($comment_id);
echo $comment_data->comment_author_email;

Get the title and link of the post from the comment

Retrieve the title and link of the post associated with a specific comment.

$comment_ID = 5;
$comentario = get_comment($comment_ID, OBJECT);
$title_post = get_the_title($comentario->comment_post_ID);
$link_post = get_permalink($comentario->comment_post_ID);