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,
));

if ($comments) {
    foreach ($comments as $comment) {
        echo $comment->comment_content;
    }
} else {
    echo 'No comments found for this post.';
}
// Example 2: Retrieve comments with specific criteria
$args = array(
    'status' => 'approve',
    'post_id' => 456,
    'number' => 5,
);

$comments = get_comments($args);

if ($comments) {
    foreach ($comments as $comment) {
        echo $comment->comment_author . ': ' . $comment->comment_content;
    }
} else {
    echo 'No approved comments found for this post.';
}
// Example 3: Retrieve comments with custom sorting
$args = array(
    'post_id' => 789,
    'orderby' => 'comment_date',
    'order' => 'ASC',
);

$comments = get_comments($args);

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