get_approved_comments()WP 2.0.0

Retrieves the approved comments for a post.

No Hooks.

Return

WP_Comment[]|Int[]|Int. The approved comments, or number of comments if $count argument is true.

Usage

get_approved_comments( $post_id, $args );
$post_id(int) (required)
The ID of the post.
$args(array)

See WP_Comment_Query::__construct() for information on accepted arguments.

Default: array()

  • status(int)
    Comment status to limit results by.
    Default: approved comments

  • post_id(int)
    Limit results to those affiliated with a given post ID.

  • order(string)
    How to order retrieved comments.
    Default: 'ASC'

Changelog

Since 2.0.0 Introduced.
Since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.

get_approved_comments() code WP 6.5.2

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 );
}