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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Try 'post_status' => 'publish', that should do the trick.
See https://developer.wordpress.org/reference/functions/get_post_statuses/ for more details.
Method 2
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');
});
?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0