users - Author comment count in author page

hi i want to show author comment count in author page. this code can display current user comment count but i dont want

hi i want to show author comment count in author page. this code can display current user comment count but i dont want current user i need any user please help me

/* user commnet count */
    function commentCount() {
        global $wpdb, $current_user;
        get_currentuserinfo();
        $userId = $current_user->ID;

        $count = $wpdb->get_var('
                 SELECT COUNT(comment_ID) 
                 FROM ' . $wpdb->comments. ' 
                 WHERE user_id = "' . $userId . '"');
        echo 'Şərh sayı ' . $count ;
    }
      <?php commentCount(); ?>

hi i want to show author comment count in author page. this code can display current user comment count but i dont want current user i need any user please help me

/* user commnet count */
    function commentCount() {
        global $wpdb, $current_user;
        get_currentuserinfo();
        $userId = $current_user->ID;

        $count = $wpdb->get_var('
                 SELECT COUNT(comment_ID) 
                 FROM ' . $wpdb->comments. ' 
                 WHERE user_id = "' . $userId . '"');
        echo 'Şərh sayı ' . $count ;
    }
      <?php commentCount(); ?>
Share Improve this question asked Feb 19, 2020 at 9:10 Gidromasservis QSCGidromasservis QSC 331 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

all you have to do is to add an param:

/* user commnet count */
function get_comment_count( $user_ID ) {
    global $wpdb;

    $count = $wpdb->get_var(
        $wpdb->prepare( "SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE user_id = %d ", $user_ID )
    );
    return $count;
}
<?php echo get_comment_count( <USER_ID> ); ?>

PS. I've added some proper escaping in your query also, so this code is not vulnerable any more...

But you can also use...

WordPress already has its own way of doing it, so you don't have to reinvent the wheel... You can use:

$count = get_comments( array(
    'user_id' => <USER_ID>, // include only comments by this user
    'count' => true // it will return only count of comments and not the comments
) );
echo $count;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744736342a4590769.html

相关推荐

  • users - Author comment count in author page

    hi i want to show author comment count in author page. this code can display current user comment count but i dont want

    10小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信