Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

get_comments

Function Description:

Retrieves a list of comments.

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Basic usage of get_comments to retrieve all comments for a specific post
$post_id = 123;
$comments = get_comments( array( 'post_id' => $post_id ) );
foreach ( $comments as $comment ) {
    echo $comment->comment_content;
}
// Using get_comments to retrieve comments with a specific status
$post_id = 456;
$approved_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
foreach ( $approved_comments as $comment ) {
    echo $comment->comment_author . ': ' . $comment->comment_content;
}
// Avoiding common pitfalls when using get_comments with pagination
$post_id = 789;
$page = 2;
$comments_per_page = 5;
$comments = get_comments( array( 'post_id' => $post_id, 'number' => $comments_per_page, 'offset' => ( $page - 1 ) * $comments_per_page ) );
foreach ( $comments as $comment ) {
    echo $comment->comment_author . ': ' . $comment->comment_content;
}