Question

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)
{ ?>
            <li>
                <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); ?>...
            </li>
        <?php
} ?>
    </ul>

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?

Was it helpful?

Solution

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

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

OTHER TIPS

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');
});
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top