admin管理员组文章数量:1323168
I try to get all comments of a specific post, but it is not working like the way I do want. I use $assignment->ID
to get all comments for a specific page ID. Even when I change $assignment->ID
to 101
it is not working and he still shows all the comments of all posts.
foreach($assignments as $assignment) {
echo $assignment->post_title;
$args = array(
'number' => 0,
'status' => 'approve',
// shows all comments, but it shouldn't
'comment_post_ID' => $assignment->ID
);
$comments = get_comments( $args );
if ( $comments ) {
foreach ( $comments as $comment ) {
echo '<li>';
echo $comment->comment_content;
echo '</li>';
}
}
}
I think I miss an important $arg
, but I'm not sure which one. It doesn't matter what I do, all comments are showing up everytime..
I try to get all comments of a specific post, but it is not working like the way I do want. I use $assignment->ID
to get all comments for a specific page ID. Even when I change $assignment->ID
to 101
it is not working and he still shows all the comments of all posts.
foreach($assignments as $assignment) {
echo $assignment->post_title;
$args = array(
'number' => 0,
'status' => 'approve',
// shows all comments, but it shouldn't
'comment_post_ID' => $assignment->ID
);
$comments = get_comments( $args );
if ( $comments ) {
foreach ( $comments as $comment ) {
echo '<li>';
echo $comment->comment_content;
echo '</li>';
}
}
}
I think I miss an important $arg
, but I'm not sure which one. It doesn't matter what I do, all comments are showing up everytime..
1 Answer
Reset to default 0Based on developer documents of get_comments
functions, it is using args like WP_Comment_Query::__construct
Method which is accepts post_id
in the argument's array. So, the code will be like:
foreach($assignments as $assignment) {
echo $assignment->post_title;
$args = array(
'number' => 0,
'status' => 'approve',
// shows all comments, but it shouldn't
'post_id' => $assignment->ID
);
$comments = get_comments( $args );
if ( $comments ) {
foreach ( $comments as $comment ) {
echo '<li>';
echo $comment->comment_content;
echo '</li>';
}
}
}
Links:
https://developer.wordpress/reference/functions/get_comments/ https://developer.wordpress/reference/classes/wp_comment_query/__construct/
本文标签: Get all comments associated with a specific page ID (commentpostID)
版权声明:本文标题:Get all comments associated with a specific page ID (comment_post_ID) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135796a2422361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论