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
- Add extra field to users
- Integrating WordPress to my website, while keeping my own authentication system
- Facebook Comment Count
- 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?
- What is the most efficient way to search users by their display name?
- Can i check if user is doing any ajax request?
- How do I create a WP user outside of WordPress and auto login?
- Network installation giving errors when viewing or adding a user
- count number of user comments with a specific comment meta value
- Change comments form title on a page by page basis
- Showing user ID on user main page from screen options
- Getting the Current User
- How do I hide specific user profile fields?
- Specific Loop For 2 Within Each
- How do I make wordpress comment fields required?
- Authenticating to WordPress, using my own authentication two-factor system
- How do I create comment-reply-button using element not
- How to change post count in wordpress loop?
- How to track a users progress through pages by inserting data into WordPress Database?
- How to check if comments are paginated?
- automated tests as a user?
- How can I default to all WordPress roles when parameter is not included in shortcode?
- there’s a way to include a minimal WP for check only the current user, its roles (caps?) and then release/free it?
- Count number of published posts by type
- Notify WordPress Site I Have Linked To Them
- Delete option value from array using update_option()
- Add “last” and “first” to WordPress Comment Pagination Links
- Uncaught TypeError: Cannot read property ‘firstChild’ of null after upgrading to WordPress 5.5
- Replace comment avatars and links at the same time
- Automatically generate Post/Page from searched Database item?
- get_users(…) only returns one user
- Adding ads code between comments
- List User order by ID in Descending order (Backend)
- Warning: Use of undefined constant HTTP_USER_AGENT – assumed ‘HTTP_USER_AGENT’ (this will throw an Error in a future version of PHP)
- How do I fetch all comments per post via WP REST API?
- Get all user meta by meta key instead of ID
- comments_number display outside specified span
- Trying to implement Ajax comments in WordPress, getting WP error
- How to get a list of all possible values of a specific user meta key?
- How does this WordPress Plugin (Thrive Comments) apply their custom comment sort? [closed]
- Adding country tags automatically
- Show comments menu in dashboard only if the site has comment
- “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
- Custom user login page by creating a plugin
- Show wp_die if url form comment not emply
- 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?
- user_profile_update_errors hook not executing
- Modify user profile data through scripting?
- How to implement a rating and review system for WordPress custom posts?
- Get users that likes the post
- Ajax load more button for comments wordpress
- custom user role wordpress – grant guest access to edit.php without insert/update/delete
- add_cap not working
- edit_comment_link is not showing for comment author
- 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
- wordpress archive a user
- wp-comments-post.php file returns a blank page
- php function to display commenter username or login
- Genesis framework comments broken?
- Best practice for show data to one specific user?
- check if comment submitted was successful
- Call to undefined function get_userdata() in plugin
- Alter required message using comment form api
- get_users meta_query: REGEXP not working for matching new lines
- Get post id within comments loop
- Change CSS based on is_user_logged_in
- How to show a users bio on a page
- “operation successful” message
- WordPress comment submit button substitutes/options
- How to list commenters and days since last commented
- php if username is role
- How to add text to the start of all comments?
- Does anyone know all parameters of get_comments()
- Appended comments but they show up above the content instead of below
- get comment data using comment id
- Removing the comment function within wordpress
- How can i count the post added in relationship field, theme display
- How do you create two separate Register pages?
- wp_delete_comment takes two tries to delete – then deletes all comments
- Change “No Comments” link to “My String” on Blog Post (Find Snippet in Code or use CSS, PHP Solution) [closed]
- 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
- Author comment count in author page
- In WordPress, how do I get the number of posts next to single_cat_title(”); in the category.php file?