The get_comments() WordPress PHP function retrieves a list of comments for the blog as a whole or for an individual post.
Usage
$comments = get_comments($args);
Parameters
$args(string|array): Optional. Array or string of arguments to filter the comments. See WP_Comment_Query::__construct() for information on accepted arguments.
More information
See WordPress Developer Resources: get_comments()
Examples
Show comment counts of a post
$args = array( 'post_id' => 1, 'count' => true ); $comments_count = get_comments($args); echo $comments_count;
Get comments from last 4 weeks
$args = array(
'date_query' => array(
'after' => '4 weeks ago',
'before' => 'tomorrow',
'inclusive' => true,
),
);
$comments = get_comments($args);
foreach ($comments as $comment) {
// Output comments etc here
}
Show comments of a specific post
$comments = get_comments(array('post_id' => 15));
foreach ($comments as $comment) {
echo $comment->comment_author;
}
Show last 5 unapproved comments
$args = array(
'status' => 'hold',
'number' => '5',
'post_id' => 1
);
$comments = get_comments($args);
foreach ($comments as $comment) {
echo $comment->comment_author . '<br />' . $comment->comment_content;
}
Show comment counts of a user
$args = array( 'user_id' => 1, 'count' => true ); $comments_count = get_comments($args); echo $comments_count;
Show comments of a user
$args = array(
'user_id' => 1
);
$comments = get_comments($args);
foreach ($comments as $comment) {
echo $comment->comment_author . '<br />' . $comment->comment_content;
}