您可以使用get_comments()
用于检索注释的函数user_id
或者您也可以修改以下内容以接受author_email
或多个作者。然后,它将只以数组形式返回注释ID并对其进行计数,返回的整数可以是0。
/**
* Count and return number of comments by User ID
*
* @param Integer $user_id - User ID of the wanted user
*
* @return Integer - Number of comments by given user
*/
function wpse284435_comment_count_by_author( $user_id ) {
$user_id = ( ! empty( $user_id ) ) ? intval( $user_id ) : get_current_user_id();
$comments = get_comments( array(
\'fields\' => \'ids\',
\'user_id\' => $user_id,
) );
return count( $comments );
}
$comment_count = wpse284435_comment_count_by_author( 7 );