WordPress.org

Codex

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

Function Reference/get comment

Description

Retrieves comment data given a comment ID or comment object. You can specify, by means of the $output parameter, how you would like the results returned.

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.

Usage

<?php get_comment$id$output ); ?> 

Parameters

$comment
(integer) (required) The ID of the comment you'd like to fetch. You must pass a variable containing an integer (e.g. $id). A literal integer (e.g. 7) will cause a fatal error (Only variables can be passed for reference or Cannot pass parameter 1 by reference).
Default: None
$output
(string) (optional) How you'd like the result. OBJECT = an object, ARRAY_A = an associative array of keys to values, and ARRAY_N = a numerically indexed array of values.
Default: OBJECT

Return

(object|array) 
On success, it returns the database fields for this comment from the wp_comments table, in the format specified. On failure, null is returned. The fields returned are:
comment_ID 
(integer) The comment ID
comment_post_ID 
(integer) The post ID of the associated post
comment_author 
(string) The comment author's name
comment_author_email 
(string) The comment author's email
comment_author_url 
(string) The comment author's webpage
comment_author_IP 
(string) The comment author's IP
comment_date 
(string) The datetime of the comment (YYYY-MM-DD HH:MM:SS)
comment_date_gmt 
(string) The GMT datetime of the comment (YYYY-MM-DD HH:MM:SS)
comment_content 
(string) The comment's contents
comment_karma 
(integer) The comment's karma
comment_approved 
(string) The comment approbation (0, 1 or 'spam')
comment_agent 
(string) The comment's agent (browser, Operating System, etc.)
comment_type 
(string) The comment's type if meaningfull (pingback|trackback), and empty for normal comments
comment_parent 
(string) The parent comment's ID
user_id 
(integer) The comment author's ID if he is registered (0 otherwise)

Example

To get the author's name of a comment with ID 7:

<?php
$my_id = 7;
$comment_id_7 = get_comment( $my_id ); 
$name = $comment_id_7->comment_author;
?> 

Alternatively, specify the $output parameter:

<?php
$my_id = 7;
$comment_id_7 = get_comment( $my_id, ARRAY_A );
$name = $comment_id_7['comment_author'];
?>
<?php
//	Correct: pass a dummy variable as post_id
$the_comment = & get_comment( $dummy_id = 7 );
	
//	Incorrect: literal integer as post_id
$the_comment = & get_comment( 7 );
//	Fatal error: 'Only variables can be passed for reference' or 'Cannot pass parameter 1 by reference'
?>

Notes

Change Log

Since: 2.0.0

Source File

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

Related

 

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.