get_approved_comments

The timeline below displays how wordpress function get_approved_comments has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.2

/**
 * Retrieves the approved comments for a post.
 *
 * @since 2.0.0
 * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
 *
 * @param int   $post_id The ID of the post.
 * @param array $args    {
 *     Optional. See WP_Comment_Query::__construct() for information on accepted arguments.
 *
 *     @type int    $status  Comment status to limit results by. Defaults to approved comments.
 *     @type int    $post_id Limit results to those affiliated with a given post ID.
 *     @type string $order   How to order retrieved comments. Default 'ASC'.
 * }
 * @return WP_Comment[]|int[]|int The approved comments, or number of comments if `$count`
 *                                argument is true.
 */
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);
}

WordPress Version: 6.1

/**
 * Retrieves the approved comments for a post.
 *
 * @since 2.0.0
 * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
 *
 * @param int   $post_id The ID of the post.
 * @param array $args    Optional. See WP_Comment_Query::__construct() for information on accepted arguments.
 * @return WP_Comment[]|int[]|int The approved comments, or number of comments if `$count`
 *                                argument is true.
 */
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);
}

WordPress Version: 5.5

/**
 * Retrieve the approved comments for post $post_id.
 *
 * @since 2.0.0
 * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
 *
 * @param int   $post_id The ID of the post.
 * @param array $args    Optional. See WP_Comment_Query::__construct() for information on accepted arguments.
 * @return int|array The approved comments, or number of comments if `$count`
 *                   argument is true.
 */
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);
}

WordPress Version: 5.3

/**
 * Retrieve the approved comments for post $post_id.
 *
 * @since 2.0.0
 * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
 *
 * @param  int   $post_id The ID of the post.
 * @param  array $args    Optional. See WP_Comment_Query::__construct() for information on accepted arguments.
 * @return int|array $comments The approved comments, or number of comments if `$count`
 *                             argument is true.
 */
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);
}

WordPress Version: 4.9

/**
 * Retrieve the approved comments for post $post_id.
 *
 * @since 2.0.0
 * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
 *
 * @param  int   $post_id The ID of the post.
 * @param  array $args    Optional. See WP_Comment_Query::__construct() for information on accepted arguments.
 * @return int|array $comments The approved comments, or number of comments if `$count`
 *                             argument is true.
 */
function get_approved_comments($post_id, $args = array())
{
    if (!$post_id) {
        return array();
    }
    $defaults = array('status' => 1, 'post_id' => $post_id, 'order' => 'ASC');
    $r = wp_parse_args($args, $defaults);
    $query = new WP_Comment_Query();
    return $query->query($r);
}

WordPress Version: 4.6

/**
 * Retrieve the approved comments for post $post_id.
 *
 * @since 2.0.0
 * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
 *
 * @param  int   $post_id The ID of the post.
 * @param  array $args    Optional. See WP_Comment_Query::query() for information on accepted arguments.
 * @return int|array $comments The approved comments, or number of comments if `$count`
 *                             argument is true.
 */
function get_approved_comments($post_id, $args = array())
{
    if (!$post_id) {
        return array();
    }
    $defaults = array('status' => 1, 'post_id' => $post_id, 'order' => 'ASC');
    $r = wp_parse_args($args, $defaults);
    $query = new WP_Comment_Query();
    return $query->query($r);
}

WordPress Version: 4.1

/**
 * Retrieve the approved comments for post $post_id.
 *
 * @since 2.0.0
 * @since 4.1.0 Refactored to leverage {@see WP_Comment_Query} over a direct query.
 *
 * @param  int   $post_id The ID of the post.
 * @param  array $args    Optional. See {@see WP_Comment_Query::query()} for information
 *                        on accepted arguments.
 * @return int|array $comments The approved comments, or number of comments if `$count`
 *                             argument is true.
 */
function get_approved_comments($post_id, $args = array())
{
    if (!$post_id) {
        return array();
    }
    $defaults = array('status' => 1, 'post_id' => $post_id, 'order' => 'ASC');
    $r = wp_parse_args($args, $defaults);
    $query = new WP_Comment_Query();
    return $query->query($r);
}

WordPress Version: 3.7

/**
 * Retrieve the approved comments for post $post_id.
 *
 * @since 2.0.0
 * @uses $wpdb
 *
 * @param int $post_id The ID of the post
 * @return array $comments The approved comments
 */
function get_approved_comments($post_id)
{
    global $wpdb;
    return $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
}