Retrieves the approved comments for a post.
Parameters
$post_id
intrequired- The ID of the post.
$args
arrayoptional- See WP_Comment_Query::__construct() for information on accepted arguments.
status
intComment status to limit results by. Defaults to approved comments.post_id
intLimit results to those affiliated with a given post ID.order
stringHow to order retrieved comments. Default'ASC'
.
Default:
array()
Source
function get_approved_comments( $post_id, $args = array() ) {
if ( ! $post_id ) {
return array();
}
$defaults = array(
'status' => 1,
'post_id' => $post_id,
'order' => 'ASC',
);
$parsed_args = wp_parse_args( $args, $defaults );
$query = new WP_Comment_Query();
return $query->query( $parsed_args );
}
Changelog
Version | Description |
---|---|
4.1.0 | Refactored to leverage WP_Comment_Query over a direct query. |
2.0.0 | Introduced. |
Example
In this example we will output a simple list of comment IDs and corresponding post IDs.