In WordPress, a comment is an entity with a complete set of attributes. You can retrieve any information about a comment in WordPress using the get_comment function. Using the get_comment function, you can retrieve post ID, comment text, commenter details, date, status, etc.
How to get full details of a comment in WordPress?
WordPress has made it pretty easy for developers to work with comment entities after version v4.4.0 release.
With the get_comment
function provided by WordPress, you can get a specific comment’s details by ID as an object or array (you can determine it).
This WP_Comment
object contains the detail of the requested comment with meaningful labels.
get_comment
function syntax
This function gets two optional parameters, $comment
and $output
.
get_comment($comment = null, $output = OBJECT)
$comment
parameter determines the comment you want to retrieve details about. This can be an ID or a WP_Comment object.
The default value for $comment
parameter is null
. This means it will retrieve the current queried comment’s details from the database if you do not specify the comment you want.
$output
parameter will determine the type of returned data from WordPress. Use OBJECT
(default), ARRAY_A
, or ARRAY_N
as the value of this optional parameter.
If the get_comment
function did not find any comment with the given ID; it will return null as result.
get_comment
Output: WP_Comment
object
Look at the WP_Comment
object attributes returned by the get_comment
function of WordPress, it contains detailed information about the specified comment:
WP_Comment Object
(
[comment_ID],
[comment_post_ID],
[comment_author]
[comment_author_email]
[comment_author_url]
[comment_author_IP]
[comment_date]
[comment_date_gmt]
[comment_content]
[comment_karma]
[comment_approved]
[comment_agent]
[comment_type]
[comment_parent]
[user_id]
)
You can get the actual text for the comment with comment_content
attribute of the WP_Comment
object. the comment_author
contains the author’s name, and comment_date
value is the date when this comment was sent to the WP blog post.
Example of getting a WP comment’s detail, using comment ID
In this example, we will retrieve a WordPress comment’s information. We will get the comment’s detail with the ID 1
, which we specified in $commentId
parameter:
$commentId = 1;
$comment = get_comment($commentId);
if($comment){
echo "Comment ID: " .$comment->comment_ID."
";
echo "Comment Post ID: " .$comment->comment_post_ID."
";
echo "Comment Author: " .$comment->comment_author."
";
echo "Comment Email: " .$comment->comment_author_email."
";
echo "Comment Author URL: " .$comment->comment_author_url."
";
echo "Comment Author IP: " .$comment->comment_author_IP."
";
echo "Comment Date and time: " .$comment->comment_date."
";
echo "Comment Date (GMT): " .$comment->comment_date_gmt."
";
echo "Comment Content (most important): " .$comment->comment_content."
";
echo "Comment Karma: " .$comment->comment_karma."
";
echo "Comment Approval Status: " .$comment->comment_approved."
";
echo "Comment Agent: " .$comment->comment_agent."
";
echo "Comment Type: " .$comment->comment_type."
";
echo "Comment parent (if it's a reply): " .$comment->comment_parent."
";
echo "Comment User (if available): " .$comment->user_id."
";
}
else{
echo "We did not find any comment with the given ID.";
}
written by Mehdi Nazari about in WordPress WordPress Functions WordPress Plugin Development WordPress Theme Development
What do you think about "How to Get Comment Details by ID in WordPress"?