get_comment

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

WordPress Version: 5.8

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 *
 * @global WP_Comment $comment Global comment object.
 *
 * @param WP_Comment|string|int $comment Comment to retrieve.
 * @param string                $output  Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 *                                       correspond to a WP_Comment object, an associative array, or a numeric array,
 *                                       respectively. Default OBJECT.
 * @return WP_Comment|array|null Depends on $output value.
 */
function get_comment($comment = null, $output = OBJECT)
{
    if (empty($comment) && isset($GLOBALS['comment'])) {
        $comment = $GLOBALS['comment'];
    }
    if ($comment instanceof WP_Comment) {
        $_comment = $comment;
    } elseif (is_object($comment)) {
        $_comment = new WP_Comment($comment);
    } else {
        $_comment = WP_Comment::get_instance($comment);
    }
    if (!$_comment) {
        return null;
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param WP_Comment $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if (OBJECT === $output) {
        return $_comment;
    } elseif (ARRAY_A === $output) {
        return $_comment->to_array();
    } elseif (ARRAY_N === $output) {
        return array_values($_comment->to_array());
    }
    return $_comment;
}

WordPress Version: 5.6

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 *
 * @global WP_Comment $comment Global comment object.
 *
 * @param WP_Comment|string|int $comment Comment to retrieve.
 * @param string                $output  Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 *                                       correspond to a WP_Comment object, an associative array, or a numeric array,
 *                                       respectively. Default OBJECT.
 * @return WP_Comment|array|null Depends on $output value.
 */
function get_comment($comment = null, $output = OBJECT)
{
    if (empty($comment) && isset($GLOBALS['comment'])) {
        $comment = $GLOBALS['comment'];
    }
    if ($comment instanceof WP_Comment) {
        $_comment = $comment;
    } elseif (is_object($comment)) {
        $_comment = new WP_Comment($comment);
    } else {
        $_comment = WP_Comment::get_instance($comment);
    }
    if (!$_comment) {
        return null;
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param WP_Comment $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if (OBJECT == $output) {
        return $_comment;
    } elseif (ARRAY_A == $output) {
        return $_comment->to_array();
    } elseif (ARRAY_N == $output) {
        return array_values($_comment->to_array());
    }
    return $_comment;
}

WordPress Version: 5.5

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 *
 * @global WP_Comment $comment Global comment object.
 *
 * @param WP_Comment|string|int $comment Comment to retrieve.
 * @param string                $output  Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 *                                       correspond to a WP_Comment object, an associative array, or a numeric array,
 *                                       respectively. Default OBJECT.
 * @return WP_Comment|array|null Depends on $output value.
 */
function get_comment(&$comment = null, $output = OBJECT)
{
    if (empty($comment) && isset($GLOBALS['comment'])) {
        $comment = $GLOBALS['comment'];
    }
    if ($comment instanceof WP_Comment) {
        $_comment = $comment;
    } elseif (is_object($comment)) {
        $_comment = new WP_Comment($comment);
    } else {
        $_comment = WP_Comment::get_instance($comment);
    }
    if (!$_comment) {
        return null;
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param WP_Comment $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if (OBJECT == $output) {
        return $_comment;
    } elseif (ARRAY_A == $output) {
        return $_comment->to_array();
    } elseif (ARRAY_N == $output) {
        return array_values($_comment->to_array());
    }
    return $_comment;
}

WordPress Version: 5.4

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 *
 * @global WP_Comment $comment Global comment object.
 *
 * @param WP_Comment|string|int $comment Comment to retrieve.
 * @param string                $output  Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 *                                       a WP_Comment object, an associative array, or a numeric array, respectively. Default OBJECT.
 * @return WP_Comment|array|null Depends on $output value.
 */
function get_comment(&$comment = null, $output = OBJECT)
{
    if (empty($comment) && isset($GLOBALS['comment'])) {
        $comment = $GLOBALS['comment'];
    }
    if ($comment instanceof WP_Comment) {
        $_comment = $comment;
    } elseif (is_object($comment)) {
        $_comment = new WP_Comment($comment);
    } else {
        $_comment = WP_Comment::get_instance($comment);
    }
    if (!$_comment) {
        return null;
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param WP_Comment $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if (OBJECT == $output) {
        return $_comment;
    } elseif (ARRAY_A == $output) {
        return $_comment->to_array();
    } elseif (ARRAY_N == $output) {
        return array_values($_comment->to_array());
    }
    return $_comment;
}

WordPress Version: 5.3

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 *
 * @global WP_Comment $comment Global comment object.
 *
 * @param WP_Comment|string|int $comment Comment to retrieve.
 * @param string                $output  Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 *                                       a WP_Comment object, an associative array, or a numeric array, respectively. Default OBJECT.
 * @return WP_Comment|array|null Depends on $output value.
 */
function get_comment(&$comment = null, $output = OBJECT)
{
    if (empty($comment) && isset($GLOBALS['comment'])) {
        $comment = $GLOBALS['comment'];
    }
    if ($comment instanceof WP_Comment) {
        $_comment = $comment;
    } elseif (is_object($comment)) {
        $_comment = new WP_Comment($comment);
    } else {
        $_comment = WP_Comment::get_instance($comment);
    }
    if (!$_comment) {
        return null;
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param mixed $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if ($output == OBJECT) {
        return $_comment;
    } elseif ($output == ARRAY_A) {
        return $_comment->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_comment->to_array());
    }
    return $_comment;
}

WordPress Version: 4.7

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 *
 * @global WP_Comment $comment
 *
 * @param WP_Comment|string|int $comment Comment to retrieve.
 * @param string                $output  Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 *                                       a WP_Comment object, an associative array, or a numeric array, respectively. Default OBJECT.
 * @return WP_Comment|array|null Depends on $output value.
 */
function get_comment(&$comment = null, $output = OBJECT)
{
    if (empty($comment) && isset($GLOBALS['comment'])) {
        $comment = $GLOBALS['comment'];
    }
    if ($comment instanceof WP_Comment) {
        $_comment = $comment;
    } elseif (is_object($comment)) {
        $_comment = new WP_Comment($comment);
    } else {
        $_comment = WP_Comment::get_instance($comment);
    }
    if (!$_comment) {
        return null;
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param mixed $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if ($output == OBJECT) {
        return $_comment;
    } elseif ($output == ARRAY_A) {
        return $_comment->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_comment->to_array());
    }
    return $_comment;
}

WordPress Version: 4.4

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 *
 * @global WP_Comment $comment
 *
 * @param WP_Comment|string|int $comment Comment to retrieve.
 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
 * @return WP_Comment|array|null Depends on $output value.
 */
function get_comment(&$comment = null, $output = OBJECT)
{
    if (empty($comment) && isset($GLOBALS['comment'])) {
        $comment = $GLOBALS['comment'];
    }
    if ($comment instanceof WP_Comment) {
        $_comment = $comment;
    } elseif (is_object($comment)) {
        $_comment = new WP_Comment($comment);
    } else {
        $_comment = WP_Comment::get_instance($comment);
    }
    if (!$_comment) {
        return null;
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param mixed $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if ($output == OBJECT) {
        return $_comment;
    } elseif ($output == ARRAY_A) {
        return $_comment->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_comment->to_array());
    }
    return $_comment;
}

WordPress Version: 4.3

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 *
 * @global wpdb   $wpdb WordPress database abstraction object.
 * @global object $comment
 *
 * @param object|string|int $comment Comment to retrieve.
 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
 * @return object|array|null Depends on $output value.
 */
function get_comment(&$comment, $output = OBJECT)
{
    global $wpdb;
    if (empty($comment)) {
        if (isset($GLOBALS['comment'])) {
            $_comment =& $GLOBALS['comment'];
        } else {
            $_comment = null;
        }
    } elseif (is_object($comment)) {
        wp_cache_add($comment->comment_ID, $comment, 'comment');
        $_comment = $comment;
    } else if (isset($GLOBALS['comment']) && $GLOBALS['comment']->comment_ID == $comment) {
        $_comment =& $GLOBALS['comment'];
    } elseif (!$_comment = wp_cache_get($comment, 'comment')) {
        $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d LIMIT 1", $comment));
        if (!$_comment) {
            return null;
        }
        wp_cache_add($_comment->comment_ID, $_comment, 'comment');
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param mixed $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if ($output == OBJECT) {
        return $_comment;
    } elseif ($output == ARRAY_A) {
        $__comment = get_object_vars($_comment);
        return $__comment;
    } elseif ($output == ARRAY_N) {
        $__comment = array_values(get_object_vars($_comment));
        return $__comment;
    } else {
        return $_comment;
    }
}

WordPress Version: 4.1

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param object|string|int $comment Comment to retrieve.
 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
 * @return object|array|null Depends on $output value.
 */
function get_comment(&$comment, $output = OBJECT)
{
    global $wpdb;
    if (empty($comment)) {
        if (isset($GLOBALS['comment'])) {
            $_comment =& $GLOBALS['comment'];
        } else {
            $_comment = null;
        }
    } elseif (is_object($comment)) {
        wp_cache_add($comment->comment_ID, $comment, 'comment');
        $_comment = $comment;
    } else if (isset($GLOBALS['comment']) && $GLOBALS['comment']->comment_ID == $comment) {
        $_comment =& $GLOBALS['comment'];
    } elseif (!$_comment = wp_cache_get($comment, 'comment')) {
        $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d LIMIT 1", $comment));
        if (!$_comment) {
            return null;
        }
        wp_cache_add($_comment->comment_ID, $_comment, 'comment');
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param mixed $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if ($output == OBJECT) {
        return $_comment;
    } elseif ($output == ARRAY_A) {
        $__comment = get_object_vars($_comment);
        return $__comment;
    } elseif ($output == ARRAY_N) {
        $__comment = array_values(get_object_vars($_comment));
        return $__comment;
    } else {
        return $_comment;
    }
}

WordPress Version: 3.9

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 * @uses $wpdb
 *
 * @param object|string|int $comment Comment to retrieve.
 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
 * @return object|array|null Depends on $output value.
 */
function get_comment(&$comment, $output = OBJECT)
{
    global $wpdb;
    if (empty($comment)) {
        if (isset($GLOBALS['comment'])) {
            $_comment =& $GLOBALS['comment'];
        } else {
            $_comment = null;
        }
    } elseif (is_object($comment)) {
        wp_cache_add($comment->comment_ID, $comment, 'comment');
        $_comment = $comment;
    } else if (isset($GLOBALS['comment']) && $GLOBALS['comment']->comment_ID == $comment) {
        $_comment =& $GLOBALS['comment'];
    } elseif (!$_comment = wp_cache_get($comment, 'comment')) {
        $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d LIMIT 1", $comment));
        if (!$_comment) {
            return null;
        }
        wp_cache_add($_comment->comment_ID, $_comment, 'comment');
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param mixed $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if ($output == OBJECT) {
        return $_comment;
    } elseif ($output == ARRAY_A) {
        $__comment = get_object_vars($_comment);
        return $__comment;
    } elseif ($output == ARRAY_N) {
        $__comment = array_values(get_object_vars($_comment));
        return $__comment;
    } else {
        return $_comment;
    }
}

WordPress Version: 3.8

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 * @uses $wpdb
 *
 * @param object|string|int $comment Comment to retrieve.
 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
 * @return object|array|null Depends on $output value.
 */
function get_comment(&$comment, $output = OBJECT)
{
    global $wpdb;
    $null = null;
    if (empty($comment)) {
        if (isset($GLOBALS['comment'])) {
            $_comment =& $GLOBALS['comment'];
        } else {
            $_comment = null;
        }
    } elseif (is_object($comment)) {
        wp_cache_add($comment->comment_ID, $comment, 'comment');
        $_comment = $comment;
    } else if (isset($GLOBALS['comment']) && $GLOBALS['comment']->comment_ID == $comment) {
        $_comment =& $GLOBALS['comment'];
    } elseif (!$_comment = wp_cache_get($comment, 'comment')) {
        $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d LIMIT 1", $comment));
        if (!$_comment) {
            return $null;
        }
        wp_cache_add($_comment->comment_ID, $_comment, 'comment');
    }
    /**
     * Fires after a comment is retrieved.
     *
     * @since 2.3.0
     *
     * @param mixed $_comment Comment data.
     */
    $_comment = apply_filters('get_comment', $_comment);
    if ($output == OBJECT) {
        return $_comment;
    } elseif ($output == ARRAY_A) {
        $__comment = get_object_vars($_comment);
        return $__comment;
    } elseif ($output == ARRAY_N) {
        $__comment = array_values(get_object_vars($_comment));
        return $__comment;
    } else {
        return $_comment;
    }
}

WordPress Version: 3.7

/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 * @uses $wpdb
 *
 * @param object|string|int $comment Comment to retrieve.
 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
 * @return object|array|null Depends on $output value.
 */
function get_comment(&$comment, $output = OBJECT)
{
    global $wpdb;
    $null = null;
    if (empty($comment)) {
        if (isset($GLOBALS['comment'])) {
            $_comment =& $GLOBALS['comment'];
        } else {
            $_comment = null;
        }
    } elseif (is_object($comment)) {
        wp_cache_add($comment->comment_ID, $comment, 'comment');
        $_comment = $comment;
    } else if (isset($GLOBALS['comment']) && $GLOBALS['comment']->comment_ID == $comment) {
        $_comment =& $GLOBALS['comment'];
    } elseif (!$_comment = wp_cache_get($comment, 'comment')) {
        $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d LIMIT 1", $comment));
        if (!$_comment) {
            return $null;
        }
        wp_cache_add($_comment->comment_ID, $_comment, 'comment');
    }
    $_comment = apply_filters('get_comment', $_comment);
    if ($output == OBJECT) {
        return $_comment;
    } elseif ($output == ARRAY_A) {
        $__comment = get_object_vars($_comment);
        return $__comment;
    } elseif ($output == ARRAY_N) {
        $__comment = array_values(get_object_vars($_comment));
        return $__comment;
    } else {
        return $_comment;
    }
}