get_approved_comments( int $post_id, array $args = array() ): WP_Comment[]|int[]|int

Retrieves the approved comments for a post.

Parameters

$post_idintrequired
The ID of the post.
$argsarrayoptional
See WP_Comment_Query::__construct() for information on accepted arguments.
  • status int
    Comment status to limit results by. Defaults to approved comments.
  • post_id int
    Limit results to those affiliated with a given post ID.
  • order string
    How to order retrieved comments. Default 'ASC'.

Default:array()

Return

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

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

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

User Contributed Notes

You must log in before being able to contribute a note or feedback.