get_comments( string|array $args = '' )
Retrieve a list of comments.
Description Description
The comment list can be for the blog as a whole or for an individual post.
Parameters Parameters
- $args
-
(string|array) (Optional) Array or string of arguments. See WP_Comment_Query::__construct() for information on accepted arguments.
Default value: ''
Return Return
(int|array) List of comments or number of found comments if $count
argument is true.
Source Source
File: wp-includes/comment.php
240 241 242 243 | function get_comments( $args = '' ) { $query = new WP_Comment_Query; return $query ->query( $args ); } |
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
2.7.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Show comment counts of a post
$args
=
array
(
'post_id'
=> 1,
// Use post_id, not post_ID
'count'
=> true
// Return only the count
);
$comments_count
= get_comments(
$args
);
echo
$comments_count
;
Example
$comments
= get_comments(
array
(
'post_id'
=> 15 ) );
foreach
(
$comments
as
$comment
) :
echo
$comment
->comment_author;
endforeach
;
Show last 5 unapproved comments
$args
=
array
(
'status'
=>
'hold'
,
'number'
=>
'5'
,
'post_id'
=> 1,
// use post_id, not post_ID
);
$comments
= get_comments(
$args
);
foreach
(
$comments
as
$comment
) :
echo
$comment
->comment_author .
'<br />'
.
$comment
->comment_content;
endforeach
;
Show comment counts of a user
$args
=
array
(
'user_id'
=> 1,
// Use user_id.
'count'
=> true
// Return only the count.
);
$comments_count
= get_comments(
$args
);
echo
$comments_count
;
Show comments of a user
<?php
$args
=
array
(
'user_id'
=> 1,
// use user_id
);
$comments
= get_comments(
$args
);
foreach
(
$comments
as
$comment
) :
echo
$comment
->comment_author .
'<br />'
.
$comment
->comment_content;
endforeach
;
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
}