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
$comments = get_comments( array(
    'post_id' => 123
) );

foreach ( $comments as $comment ) {
    echo $comment->comment_content;
}
// Example 2: Retrieve comments with specific meta data
$comments = get_comments( array(
    'meta_key' => 'rating',
    'meta_value' => '5',
) );

if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo $comment->comment_content;
    }
} else {
    echo 'No comments found with rating 5.';
}
// Example 3: Retrieve comments with pagination
$comments_per_page = 10;
$page = 1;

$comments = get_comments( array(
    'number' => $comments_per_page,
    'offset' => ( $page - 1 ) * $comments_per_page,
) );

if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo $comment->comment_content;
    }
} else {
    echo 'No comments found for this page.';
}