get_comment()WP 2.0.0

Retrieves comment data given a comment ID or comment object.

If an object is passed then the comment data will be cached and then returned after being passed through a filter. If the comment is empty, then the global comment variable will be used, if it is set.

1 time — 0.000436 sec (fast) | 50000 times — 0.38 sec (very fast) | PHP 7.1.2RC1, WP 4.7.2
Hooks from the function

Return

WP_Comment|Array|null. Depends on $output value.

Usage

get_comment( $comment, $output );
$comment(WP_Comment|string|int)
Comment to retrieve.
Default: null
$output(string)
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

Examples

0

#1 Get the comment - example of what the function returns

$comm_id = 11612;
$comm = get_comment( $comm_id ); 

print_r( $comm );

/* Will display
WP_Comment Object
(
	[comment_ID] => 11612
	[comment_post_ID] => 3477
	[comment_author] => Mseo
	[comment_author_email] => [email protected]
	[comment_author_url] => http://maeo.com
	[comment_author_IP] => 95.79.52.2
	[comment_date] => 2015-09-01 16:28:33
	[comment_date_gmt] => 2015-09-01 11:28:33
	[comment_content] => Greetings, the above comment date output code 
	[comment_karma] => 0
	[comment_approved] => 1
	[comment_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36
	[comment_type] => comment
	[comment_parent] => 0
	[user_id] => 0
	[children:protected] => 
	[populated_children:protected] => 
	[post_fields:protected] => Array
		(
			[0] => post_author
			[1] => post_date
			[2] => post_date_gmt
			[3] => post_content
			[4] => post_title
			[5] => post_excerpt
			[6] => post_status
			[7] => comment_status
			[8] => ping_status
			[9] => post_name
			[10] => to_ping
			[11] => pinged
			[12] => post_modified
			[13] => post_modified_gmt
			[14] => post_content_filtered
			[15] => post_parent
			[16] => guid
			[17] => menu_order
			[18] => post_type
			[19] => post_mime_type
			[20] => comment_count
		)

)
*/
0

#2 Get the name of the commenter of comment 27:

$comm_id = 27;
$comment = get_comment( $comm_id );
$name = esc_html( $comment->comment_author );
0

#3 We get the data in the form of an associative array:

$comm_id = 27;
$comment = get_comment( $comm_id, ARRAY_A );
$comment_author_name = esc_html( $comment['comment_author'] );

Notes

  • Global. WP_Comment. $comment Global comment object.

Changelog

Since 2.0.0 Introduced.

get_comment() code WP 6.4.3

function get_comment( $comment = null, $output = OBJECT ) {
	if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {
		$comment = $GLOBALS['comment'];
	}

	if ( $comment instanceof WP_Comment ) {
		$_comment = $comment;
	} elseif ( is_object( $comment ) ) {
		$_comment = new WP_Comment( $comment );
	} else {
		$_comment = WP_Comment::get_instance( $comment );
	}

	if ( ! $_comment ) {
		return null;
	}

	/**
	 * Fires after a comment is retrieved.
	 *
	 * @since 2.3.0
	 *
	 * @param WP_Comment $_comment Comment data.
	 */
	$_comment = apply_filters( 'get_comment', $_comment );

	if ( OBJECT === $output ) {
		return $_comment;
	} elseif ( ARRAY_A === $output ) {
		return $_comment->to_array();
	} elseif ( ARRAY_N === $output ) {
		return array_values( $_comment->to_array() );
	}
	return $_comment;
}