get_comment()
Gets the data of the specified comment from the database. Returns an instance of the WP_Comment class (it can be conditionally said that all fields of the wp_comments table are returned).
You can specify in what format to return the data (as an object/array). This is indicated in the second parameter $output.
If no parameters were passed, the global variable $comment will be used to determine the returned data.
Used By: get_comment_text(), get_comment_author()
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
Returns
WP_Comment|Array|null.
- WP_Comment or
arrayof comment data, depending on the $output parameter. null- if the comment could not be retrieved.
Usage
$comment = get_comment( $comment, $output );
- $comment(integer/WP_Comment)
ID/object of the comment whose data needs to be retrieved.
You need to pass a variable, not an integer (see examples below); if you pass an integer, for example 24, the function will throw an error.
Default: null
- $output(string)
The format of the returned data, can be:
OBJECT- data will be returned as an object;ARRAY_A- data will be returned as an associative array, with keys and corresponding values;ARRAY_N- data will be returned as a regular array (keys are overwritten).
Default: OBJECT
Examples
#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 ) ) */
#2 Get the name of the commenter of comment 27:
$comm_id = 27; $comment = get_comment( $comm_id ); $name = esc_html( $comment->comment_author );
#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.
$commentGlobal comment object.
Changelog
| Since 2.0.0 | Introduced. |