get_approved_comments() WordPress Function

The get_approved_comments() function retrieves all approved comments for a given post. Approved comments are those that have been approved by a moderator or administrator. This function is useful for retrieving comments that are safe to display to visitors.

get_approved_comments( int $post_id, array $args = array() ) #

Retrieves the approved comments for post $post_id.


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()


Top ↑

Return

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


Top ↑

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


Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More