All users are registered to comment on the site, so what I want to achieve is, to display user details like the meta info, description, url, etc... So, if possible also the number of posts and comments published in the comment.php template.

Any help will be appreciated.

1 Answer 1


Getting user's metas values :

$user_meta = get_user_meta($user_id, 'YOUR_META_KEY', TRUE); //You have to set the $user_id / And the "YOUR_META_KEY" can be description, name, custom meta...

To get user's comments count

function commentCount($userId = null) {
    global $wpdb;

    if($userId == null) return 0;

    $count = $wpdb->get_var('
             SELECT COUNT(comment_ID) 
             FROM ' . $wpdb->comments. ' 
             WHERE user_id = "' . $userId . '"');
    return (int)$count;

Finally, to get user's posts count

$user_post_count = count_user_posts( $userId , 'post' );
  • First of all, thx for the answer. Finally I found that the key most importantly to this question, is to get author comment id by comment id. So I used this to get author comment id $comment_id = $commentID; $comment = get_comment( $comment_id ); $comment_author_id = $comment -> user_id;
    – p oneland
    Oct 12, 2018 at 18:05
  • With the $comment_author_id, getting user's meta values and posts count work well. However getting user's comments count just failed n don't know how. Forgive me if I did not get your really point since I'm a newbie. I found it can work with global $wpdb; $where = 'WHERE comment_approved = 1 AND user_id = ' . $comment_author_id ; $comment_count = $wpdb->get_var( "SELECT COUNT( * ) AS total FROM {$wpdb->comments} {$where} ");
    – p oneland
    Oct 12, 2018 at 18:17
  • Sry for the bad formatting, so complicated for me.
    – p oneland
    Oct 12, 2018 at 18:20
  • @poneland As I mentioned above, the most important thing is to have and set the ID of the user, no matter how you do it to get that ID... Oct 15, 2018 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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