0

When I use the following custom query

$wp_comments = $wpdb->get_results("SELECT COUNT(*) FROM wp_comments WHERE userid=$userid")

If I use

print_r(wp_comments)

I get the following printed

Array ( [0] => stdClass Object ( [COUNT(*)] => 10 ) ) 

10 is the count I am expecting, how to print out the value?

Is there any other way to get the number of comments made by the user on the whole site?

1 Answer 1

7

You can use get_comments function to retrive the comment count.Pass the user id of perticuler user as an argument.

$args = array(
    'user_id' => 1, // use user id
    'count' => true //return only the count
    );
$comments = get_comments($args);
echo $comments

Please refer below link for more information. http://codex.wordpress.org/Function_Reference/get_comments

2
  • That Worked. But i have a custom db table wp_ratings created by wp_postratings plugin, I need to get total number of ratings made by the logged in user. the table have "rating_userid" column that logs the userid. how to do that?
    – Jagan K
    Oct 9, 2013 at 11:08
  • @JaganK '$wp_comments = $wpdb->get_results("SELECT COUNT(*) as comment_count FROM wp_comments WHERE userid=$userid")' echo $wp_comments[0]->comment_count; use this code.
    – dipali
    Oct 9, 2013 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.