admin管理员组文章数量:1323707
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?
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?
2 Answers
Reset to default 1Try 'post_status' => 'publish',
that should do the trick.
See https://developer.wordpress/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');
});
?>
本文标签: loopgetcomments with poststatus 39public39 retrieves NULL result
版权声明:本文标题:loop - get_comments with post_status 'public' retrieves NULL result 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742130709a2422145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论