count the total number of comments the user has received for his published posts

I want to count the total number of comments the user has received for his published posts. for example show:

total comments of your all posts : 47


Is there a way to do this? Can you help me?

Topic comments users publish posts Wordpress

Category Web


You can use the get_comments() function like so:

$total = get_comments( array(
    'post_author' => get_current_user_id(),
    'post_status' => 'publish',
    'type'        => 'comment',
    'count'       => true,
) );

echo 'total comments of your all posts : ' . $total;

Just customize the arguments based on your requirements/preferences, and in the above example, I'm retrieving the total number of comments for published posts where the author is the currently logged-in user, but only for comments of the comment type.

And if you want, you can use the status argument to limit to certain status only, e.g. approve to count approved comments only.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.