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?
// Example 1: Retrieve all comments for a specific post
$post_id = 123;
$comments = get_comments( array(
    'post_id' => $post_id,
    'status' => 'approve' // Only retrieve approved comments
) );
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo $comment->comment_content;
    }
} else {
    echo 'No comments found for post ID ' . $post_id;
}
// Example 2: Retrieve comments based on specific criteria
$comments = get_comments( array(
    'author_email' => 'john.doe@example.com',
    'status' => 'approve' // Only retrieve approved comments
) );
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo $comment->comment_content;
    }
} else {
    echo 'No comments found for author email john.doe@example.com';
}
// Example 3: Retrieve comments with pagination
$page = 1;
$comments_per_page = 10;
$comments = get_comments( array(
    'number' => $comments_per_page,
    'offset' => ( $page - 1 ) * $comments_per_page,
    'status' => 'approve' // Only retrieve approved comments
) );
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo $comment->comment_content;
    }
} else {
    echo 'No comments found for page ' . $page;
}