You are more or less there already with the code, you just need to change the code slightly.
You do not mention how you get the user id, but if you have the user ID you want to check, it is quite easy to do outside the loop.
Here is a simple example:
$userid=1;
$comment_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) AS total FROM $wpdb->comments WHERE comment_approved = 1 AND user_id = %s", $userid ) );
echo "Number of comments for user $userid is $comment_count";
Note: the part of the query that says “comment_approved = 1” counts only the approved comments, if you want to include the non-approved comments leave out that part.
Related Posts:
- User’s total comment count excluding comments on their own posts
- Create a global variable for use in all templates
- Getting the comment number relative to all the post’s comments
- Changing user_nicename
- How to auto login user again after change user_login
- How to output a permanently delete comment link?
- Can i check if user is doing any ajax request?
- How do I create a WP user outside of WordPress and auto login?
- count number of user comments with a specific comment meta value
- Change comments form title on a page by page basis
- How do I make wordpress comment fields required?
- How to change post count in wordpress loop?
- How to track a users progress through pages by inserting data into WordPress Database?
- Notify WordPress Site I Have Linked To Them
- Replace comment avatars and links at the same time
- Adding ads code between comments
- Warning: Use of undefined constant HTTP_USER_AGENT – assumed ‘HTTP_USER_AGENT’ (this will throw an Error in a future version of PHP)
- comments_number display outside specified span
- Trying to implement Ajax comments in WordPress, getting WP error
- “comments.php” not available after edit in WP administration
- User management system similar to wordpress one?
- Show all posts by author
- Sending data from custom inputs in WordPress comment form in the admin notification email
- Why does my callback function not work with this custom filter hook?
- Customize Theme comment template to Insert VoteUp and VoteDown buttons
- Programmatic Login from 3rd Party site
- Add New User, extra fields which are required?
- Modify user profile data through scripting?
- How to implement a rating and review system for WordPress custom posts?
- Ajax load more button for comments wordpress
- Limit 1 global comment per minute
- Create Page To Count Comments For Each Post In WordPress Site
- Do not execute If User is login as Administrator or any Specific Role
- comments sorting incorrectly across multiple pages
- Commenting out unwated CSS/scripts in HTML with PHP
- wp-comments-post.php file returns a blank page
- php function to display commenter username or login
- Best practice for show data to one specific user?
- check if comment submitted was successful
- Alter required message using comment form api
- get_users meta_query: REGEXP not working for matching new lines
- “operation successful” message
- php if username is role
- Does anyone know all parameters of get_comments()
- Removing the comment function within wordpress
- How can i count the post added in relationship field, theme display
- Is it possible to create new user from external form using REST API?
- Foreach loop inside an array_merge
- Send a mail to specific address in a custom field when a new comment is made on a specific post
- Show current user posts only
- How can I count ACF sub_field with a certain value
- Change page password every day (predictable password ;) )
- show something only when user comes from specific page at remote host?
- Fetching Initials of the Commentator in the WordPress Website
- How can I include the user id inside of a shortcode php output?
- Show site content based on user role
- Restrict characters in comment section
- Conditionally load class in the comment section of the post page
- Comment_author_url doing nothing
- Fatal error: Cannot redeclare comment_theme()
- Sort new column in Users wp-admin
- Sort column in Users admin Tab
- Woocommerce display orders with products from specific categories to specific admins
- Show all people who commented on all posts by an author
- Commenting requires wordpress login, wordpress discussion and post comments tick boxes are checked
- How to count active users on a specific page
- How can I show how much water got saved on this order (making use of the number of items bought) on a thank-you-page?
- Call WP Rest-Api to GET /users/me returned NOTHING in console
- If user has clicked link add class and store using PHP/WordPress
- Fetch commens from a specific post
- Store data from JavaScript object to custom table in user account
- How to change the value of a variable using input field?
- Closing Comments conditionally in comments.php
- How do I track which user clicked my button?
- Use WP user status (logged_in) to manage access to independent application
- Query to show average # of months all accounts with specific role have been active
- Editable Student file associated with basic User ID
- Adding users to another blog
- How can I output the slug of a comment author?
- Matching multi user
- How can I enqueue comment-reply script only on certain page?
- Private messaging – Getting and displaying the avatar/url of a message recipient
- Updating custom user meta
- For each 3 posts, show a different post type
- WP_Query count of different meta key values [duplicate]
- Update only some custom user fields
- divide custom field values in div every two values
- main menu page redirects to user ID
- Adapting a php array to WordPress
- Get author meta of all writers
- WordPress – show number of comments for each post in widget
- How does add_action ‘comment_post’ work?
- WordPress most commented posts of last X days
- How to add together (get the sum) a field from all author’s posts
- How do I call comments_template(); from a plugin file?
- Forbid certain users to access a specific page
- How to create custom user role without plugin?
- PHP Works only for logged in users: [closed]
- Custom Admin Menu Report for Specific User ID
- Count the number of matching post names in foreach loop