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?
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.
,
after the 'publish'
. So please try again?
,
). So let me know how it goes, even if it doesn't work for you.