如何计算特定用户的评论总数

时间:2013-10-09 作者:Jagan K

当我使用以下自定义查询时

$wp_comments = $wpdb->get_results("SELECT COUNT(*) FROM wp_comments WHERE userid=$userid")
如果我使用

print_r(wp_comments)
我打印了以下内容

Array ( [0] => stdClass Object ( [COUNT(*)] => 10 ) ) 
10是我期望的计数,如何打印出值?

有没有其他方法可以获得用户在整个网站上的评论数?

1 个回复
最合适的回答,由SO网友:dipali 整理而成

您可以使用get_comments 函数检索注释计数。将Perticular用户的用户id作为参数传递。

$args = array(
    \'user_id\' => 1, // use user id
    \'count\' => true //return only the count
    );
$comments = get_comments($args);
echo $comments
有关更多信息,请参阅下面的链接。http://codex.wordpress.org/Function_Reference/get_comments

结束