Php 如何计算用户评论了多少篇文章?
我知道如何获取特定用户写的所有评论的数量,但如何计算用户对帖子的评论数量(在WordPress中)。如果要计算用户对帖子的评论: 您可以使用Php 如何计算用户评论了多少篇文章?,php,wordpress,count,Php,Wordpress,Count,我知道如何获取特定用户写的所有评论的数量,但如何计算用户对帖子的评论数量(在WordPress中)。如果要计算用户对帖子的评论: 您可以使用get_comments()函数 它接受一个参数数组 在arguments数组中,使用user\u id仅获取此用户的注释。您还可以使用count返回注释数 代码如下所示: <?php $comments_args = array( 'user_id' => 12, // Your user ID, for exa
get_comments()
函数
它接受一个参数数组
在arguments数组中,使用user\u id
仅获取此用户的注释。您还可以使用count
返回注释数
代码如下所示:
<?php
$comments_args = array(
'user_id' => 12, // Your user ID, for example user with id 12
'count' => true // Return count
);
$comments_count = get_comments( $comments_args );
echo $comments_count;
?>
现在可以使用
echo$计数器显示数字代码>谢谢你的回答。你的代码可以工作,但这不是我想要的。我不想得到用户发表评论的数量。我需要获得用户添加了至少一条评论的帖子数量。我在一篇帖子中添加了2条评论。用你的代码,返回的数字是2而不是1。啊,好的,我想我知道了。看看我编辑过的答案。很抱歉没有听到你的问题。
$args = array(
'post_type' => 'post',
'posts_per_page' =>-1,
'post__in' => array_unique(
wp_list_pluck(
get_comments( array(
'user_id' => get_current_user_id()
)
),
'comment_post_ID'
)
),
);
$comment_query = new WP_Query( $args );
// The Loop
$counter = 0;
if ( $comment_query ->have_posts() ) {
while ( $comment_query ->have_posts() ) {
$comment_query ->the_post();
$counter++;
// or display title or something
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}