This is my loop:

<?php $comments = get_comments(array(
    'status' => 'approve',
    'type' => 'comment',
    'number' => 10,
    'post_status' => 'public'
)); ?>

    <ul class="sidebar-comments">
        <?php foreach ($comments as $comment)
{ ?>
                <div><?php echo get_avatar($comment, $size = '35'); ?></div>
                <em style="font-size:12px"><?php echo strip_tags($comment->comment_author); ?></em> (<a href="<?php echo get_option('home'); ?>/?p=<?php echo ($comment->comment_post_ID); ?>/#comment-<?php echo ($comment->comment_ID); ?>">link</a>)<br>
                <?php echo wp_html_excerpt($comment->comment_content, 35); ?>...
} ?>

This always gives an empty result (no errors). If I remove 'post_status' => 'public' from the get_comments arguments, the function works, comments load but also comments from private posts (which I don't want).

Any ideas on why 'post_status' => 'public' is not working?


Try 'post_status' => 'publish', that should do the trick.

See https://developer.wordpress.org/reference/functions/get_post_statuses/ for more details.


I am not sure if public is a valid status, but you could just filter out the private comments before the actual loop:

<?php $comments = get_comments(array(
  'status' => 'approve',
  'type' => 'comment',
  'number' => 10,
  'post_status' => 'publish'
$comments = array_filter($comments, function ($item)) {
  return ($item->comment_status !== 'private');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.