get_comments()
Retrieve a list of comments.
The comment list can be for the blog as a whole or for an individual post.
No Hooks.
Return
WP_Comment[]|Int[]|Int
. List of comments or number of found comments if $count argument is true.
Usage
get_comments( $args );
- $args(string|array)
- Array or string of arguments. See WP_Comment_Query::__construct() for information on accepted arguments.
Default: empty string
Examples
#1 Outputs all comments on post 15 (including spam and unapproved)
$comments = get_comments( 'post_id=15' ); foreach( $comments as $comment ){ echo( $comment->comment_author ); }
#2 Show 5 unapproved comments.
$comments = get_comments( [ 'status' => 'hold', 'number' => '5', 'post_id' => 1, // correctly post_id, not post_ID ] ); foreach( $comments as $comment ){ echo $comment->comment_author . '<br />' . $comment->comment_content; }
#3 Display the number of comments on the post
$comments = get_comments( [ 'post_id' => 1, // use post_id, not post_ID 'count' => true // returns only count ] ); echo $comments;
#4 Display the number of the user comments
$args = array( 'user_id' => 1, 'count' => true ); $comments = get_comments( $args ); echo $comments;
#5 Display the user's comments.
$args = array( 'user_id' => 1, // user ID ); $comments = get_comments( $args ); foreach( $comments as $comment ){ echo $comment->comment_author . '<br />' . $comment->comment_content; }
#6 Delete, in a row, identical comments.
Comparing the author and the content of the comment.
// get all comments with empty argument number $all_comments = get_comments( [ 'status' => 'approve', 'number' => '', ] ); // array that will contain the IDs of duplicate comments $comment_ids_to_delete = []; foreach( $all_comments as $k => $c ){ $kk = $k-1; // Previous comment index in the $all_comments array $pc = $all_comments[ $kk ]; // previous comment object // If the author and content are the same, add the comment to the array for deletion if( $pc->comment_author === $c->comment_author && $pc->comment_content === $c->comment_content ){ $comment_ids_to_delete[] = $pc->comment_ID; } } // Delete comments by ID foreach( $comment_ids_to_delete as $comm_id ){ wp_delete_comment( $comm_id ); }
#7 Get the latest comments in a list style.
This code gets the last 10 comments as a UL list. Where the comment text is shortened to 50 characters and is a link to the comment itself:
$args = array( 'number' => 10, 'orderby' => 'comment_date', 'order' => 'DESC', 'type' => '', // only comments, no pings, etc... ); if( $comments = get_comments( $args ) ){ echo '<ul>'; foreach( $comments as $comment ){ $comm_link = get_comment_link( $comment->comment_ID ); // can be a heavy request ... $comm_short_txt = mb_substr( strip_tags( $comment->comment_content ), 0, 50 ) .'...'; echo '<li>'. $comment->comment_author .': <a rel="nofollow" href="'. $comm_link .'">'. $comm_short_txt .'</a></li>'; } echo '</ul>'; } /* Data in $comment object stdClass Object [comment_ID] => 9727 [comment_post_ID] => 477 [comment_author] => Andrew [comment_author_email] => [email protected] [comment_author_url] => [comment_author_IP] => 178.45.177.200 [comment_date] => 2015-22-01 00:27:04 [comment_date_gmt] => 2015-22-28 21:27:04 [comment_content] => comment text [comment_karma] => 0 [comment_approved] => 1 [comment_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) [comment_type] => [comment_parent] => 9724 [user_id] => 313 */
#8 An example of getting child comments as a tree
An example of how the parameter 'hierarchical' => 'threaded'
works:
$comments = get_comments( [ 'parent' => 22384, 'hierarchical' => 'threaded', ] ); /* Result (some fields of WP_Comment object are removed for clarity): Array [22986] => WP_Comment Object [comment_ID] => 22986 [comment_post_ID] => 8342 [comment_author] => camp [comment_date] => 2018-04-11 19:07:08 [comment_content] => Added. [comment_type] => [comment_parent] => 22430 [populated_children:protected] => 1 [children:protected] => Array [22998] => WP_Comment Object [comment_ID] => 22998 [comment_post_ID] => 8342 [comment_author] => Maxim [comment_date] => 2018-04-12 12:15:25 [comment_content] => Works great. Thank you! [comment_type] => [comment_parent] => 22986 [populated_children:protected] => 1 [children:protected] => Array [23035] => WP_Comment Object [comment_ID] => 23035 [comment_post_ID] => 8342 [comment_author] => camp [comment_date] => 2018-04-16 15:36:00 [comment_content] => Thanks for the feedback! [comment_type] => [comment_parent] => 22998 [children:protected] => [populated_children:protected] => 1 [22982] => WP_Comment Object [comment_ID] => 22982 [comment_post_ID] => 8342 [comment_author] => Maxim [comment_date] => 2018-04-11 12:12:00 [comment_content] => > Great, if we could also write the output of the total rating. [comment_type] => [comment_parent] => 22430 [populated_children:protected] => 1 [children:protected] => Array [22985] => WP_Comment Object [comment_ID] => 22985 [comment_post_ID] => 8342 [comment_author] => camp [comment_date] => 2018-04-11 19:05:57 [comment_content] => Hello. Judging by the question, you don't need advice, you need to make a ready-made one. [comment_type] => [comment_parent] => 22982 [children:protected] => [populated_children:protected] => 1 */
#9 Get comments via WP_Comment_Query class
This kind of query can be useful when we need to access the properties of the object WP_Comment_Query.
For example, in the properties we can see the query that is obtained with the specified query parameters.
$args = [ 'meta_query' => [ [ 'key' => 'condition', 'value' => 'to_delete' ] ], 'date_query' => [ [ 'before' => '5 month ago' ] ], 'orderby' => [ 'comment_ID' => 'DESC' ], 'number' => 100 ]; $query = new WP_Comment_Query; $comments = $query->query( $args ); // output the query echo $query->request; /* SELECT wp_comments.comment_ID FROM wp_comments INNER JOIN wp_commentmeta ON ( wp_comments.comment_ID = wp_commentmeta.comment_id ) WHERE ( ( comment_approved = '0' OR comment_approved = '1' ) ) AND ( ( wp_commentmeta.meta_key = 'condition' AND wp_commentmeta.meta_value = 'to_delete' ) ) AND ( wp_comments.comment_date < '2020-02-26 11:48:03' ) GROUP BY wp_comments.comment_ID ORDER BY wp_comments.comment_ID DESC LIMIT 0,100 */
#10 Creating comment pagination
The code below shows how to display user comments with pagination links in WordPress. To do this, we need to use the WP_Comment_Query class.
Suppose we display comments on page /user-comments
and we will use GET parameter ?pagenum=10
to add pagination page number. We are going to output 50 comments per page. We are going to use function paginate_links() to create HTML code for pagination links.
$per_page = 50; $pagenum = $_GET['pagenum'] ?? 1; $offset = ($pagenum - 1) * $per_page; $paged_url_patt = home_url( preg_replace( '/[?&].*/', '', $_SERVER['REQUEST_URI'] ) ) .'?pagenum=%#%'; $query = new WP_Comment_Query; $comments = $query->query( [ 'order' => 'DESC', 'user_id' => $user_id, 'offset' => $offset, 'number' => $per_page, 'no_found_rows' => false, ] ); //$total_comments = (int) $query->found_comments; $max_pages = (int) $query->max_num_pages; $paginate_links = paginate_links( [ 'base' => $paged_url_patt, 'current' => $pagenum, 'total' => $max_pages ] ); echo $paginate_links;
The result will be the following HTML code:
<a class="prev page-numbers" href="https://example.com/profile/comments?pagenum=2">← Prev</a> <a class="page-numbers" href="https://example.com/profile/comments?pagenum=1">1</a> <a class="page-numbers" href="https://example.com/profile/comments?pagenum=2">2</a> <span aria-current="page" class="page-numbers current">3</span> <a class="page-numbers" href="https://example.com/profile/comments?pagenum=4">4</a> <a class="page-numbers" href="https://example.com/profile/comments?pagenum=5">5</a> <span class="page-numbers dots">…</span> <a class="page-numbers" href="https://example.com/profile/comments?pagenum=75">75</a> <a class="next page-numbers" href="https://example.com/profile/comments?pagenum=4">Next →</a>
#11 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 }
#12 Get all comments of two post types:
$paged = get_query_var( 'page' ) ?: 1; $args = array( 'number' => 5, 'post_type' => array( 'property', 'page' ), 'paged' => $paged, 'parent' => 0, ); $comments = get_comments( $args );
#13 Get child comment of parent comment
$args = array( 'parent' => $comment->comment_ID, //Comment ID '64' 'hierarchical' => true, 'status' => 'approve', ); $child_comments = get_comments( $args );
or you can use get_children()
$comment->get_children()
Changelog
Since 2.7.0 | Introduced. |
get_comments() get comments code WP 6.7.1
function get_comments( $args = '' ) { $query = new WP_Comment_Query(); return $query->query( $args ); }