get_comments

Home » Functions » get_comments

The get_comments function in WordPress is used to retrieve a list of comments from the database. This function returns an array of WP_Comment objects that represent the comments.

The get_comments function can be used with a variety of arguments to retrieve different sets of comments. The most common argument is ‘post_id’, which allows you to retrieve comments for a specific post. You can also use arguments such as ‘status’ and ‘number’ to filter and limit the comments that are returned.

Here is an example usage code for the get_comments function:

$comments = get_comments( array(
    'post_id' => 123,
    'status' => 'approve',
    'number' => 10,
) );

foreach ( $comments as $comment ) {
    echo '<p>' . $comment->comment_content . '</p>';
}

In this example, the get_comments function is used to retrieve up to 10 approved comments for a post with an ID of 123. The retrieved comments are then output using a foreach loop to display each comment’s content.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.