get_approved_comments()WP 2.0.0

Retrieves the approved comments for a post.

Хуков нет.

Возвращает

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

Использование

get_approved_comments( $post_id, $args );
$post_id(int) (обязательный)
The ID of the post.
$args(массив)

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

По умолчанию: array()

  • status(int)
    Comment status to limit results by.
    По умолчанию: approved comments

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

  • order(строка)
    How to order retrieved comments.
    По умолчанию: 'ASC'

Список изменений

С версии 2.0.0 Введена.
С версии 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.

Код get_approved_comments() WP 6.6.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 );
}