get_approved_comments( int $post_id, array $args = array() )
Retrieve the approved comments for post $post_id.
Description Description
Parameters Parameters
- $post_id
-
(int) (Required) The ID of the post.
- $args
-
(array) (Optional) See WP_Comment_Query::__construct() for information on accepted arguments.
Default value: array()
Return Return
(int|array) $comments The approved comments, or number of comments if $count argument is true.
Source Source
File: wp-includes/comment.php
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 );
}
Expand full source code Collapse full source code View on Trac
Changelog Changelog
| Version | Description |
|---|---|
| 4.1.0 | Refactored to leverage WP_Comment_Query over a direct query. |
| 2.0.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Example
In this example we will output a simple list of comment IDs and corresponding post IDs.
<?php $postID = 1; $comment_array = get_approved_comments($postID); foreach($comment_array as $comment){ echo $comment->comment_ID." => ".$comment->comment_post_ID."\n"; } ?>