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,
));
foreach ($comments as $comment) {
    echo $comment->comment_content;
}
// Example 2: Retrieve comments with specific criteria
$args = array(
    'status' => 'approve',
    'number' => 5,
);
$comments = get_comments($args);
foreach ($comments as $comment) {
    echo $comment->comment_author . ': ' . $comment->comment_content;
}
// Example 3: Avoid common pitfall of not specifying post ID
// When not specifying post ID, get_comments will return all comments on the site
$post_id = 123;
$comments = get_comments(array(
    'post_id' => $post_id,
));
foreach ($comments as $comment) {
    echo $comment->comment_content;
}