In administration, how do I display comments of a certain user?

From the users section, I can see a link to their posts, but not their comments. When I go to comments, there doesn’t seem to be a straightforward way to filter only comments by a specific user. How to do it? (I’m sure such basic feature must be somewhere, I just can’t find it.)

1 Answer
1

Afaik, there’s no straight forward way for this.

You’d have to extend the WP_List_Table and add an extra column. This would be an example function:

function wpse_get_comments_by_user( $user_ID )
{
    global $wpdb;

    $rows = $wpdb->query( $wpdb->prepare(
         "
            SELECT * 
            FROM %s
            WHERE user_id = %d
         "
        ,$wpdb->comments
        ,(int) $user_ID
    ) );

   return $rows;
}

The above ↑ function gives you back all DB rows for comments by the user you specified by her/his ID.

You then can go and list for e.g. all posts in a foreach loop:

foreach ( wpse_get_comments_by_user( $user->ID ) as $comment )
{
    $post_link = get_permalink( $comment->comment_post_ID );

    echo "<a href="https://wordpress.stackexchange.com/questions/43940/{$post_link}">";
        // apply the excerpt filters in case the comment content is too long
        echo apply_filters( 'the_excerpt', $comment->comment_content );
        // Add the date
        echo $comment->comment_date;
    echo '</a>';
}

The actual problem is that there’s no admin screen where you can list comments by user. So I’d say it’d be easier to do this with a link and a page-template where you hide the contents behind current_user_can( 'manage_options' );.

Leave a Comment