# Comments Column in the Users List

> Adds a "Comments" column to the Users screen showing how many approved comments each user has written, linked to their comments.

- Type: PHP
- Runs: Admin only
- Category: [Users](https://wpide.com/library/category/users/)
- Tags: users, comments, admin
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/comments-count-users-column/

## Code

```php
$wpide_user_comment_counts = null;

add_filter( 'manage_users_columns', function ( $columns ) {
    $columns['wpide_comments'] = 'Comments';

    return $columns;
} );

add_filter( 'manage_users_custom_column', function ( $output, $column, $user_id ) use ( &$wpide_user_comment_counts ) {
    global $wpdb, $wp_list_table;

    if ( 'wpide_comments' !== $column ) {
        return $output;
    }

    if ( null === $wpide_user_comment_counts ) {
        $ids = isset( $wp_list_table->items ) ? array_map( 'intval', wp_list_pluck( (array) $wp_list_table->items, 'ID' ) ) : array();
        $ids = array_filter( array_merge( $ids, array( (int) $user_id ) ) );

        $rows = $wpdb->get_results( "SELECT user_id, COUNT(*) AS total FROM {$wpdb->comments} WHERE comment_approved = '1' AND user_id IN (" . implode( ',', $ids ) . ') GROUP BY user_id' );
        $wpide_user_comment_counts = wp_list_pluck( (array) $rows, 'total', 'user_id' );
    }

    $count = (int) ( $wpide_user_comment_counts[ $user_id ] ?? 0 );

    return $count ? '<a href="' . esc_url( admin_url( 'edit-comments.php?user_id=' . (int) $user_id ) ) . '">' . esc_html( number_format_i18n( $count ) ) . '</a>' : '0';
}, 10, 3 );
```

## Notes

Counts for the whole page of users are fetched with one query.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Comments Column in the Users List" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
