Comment visibility

By using the ‘pre_get_comments’ action, found in wp-includes/comment.php:

function restrict_visible_comments( $comments_query ) {
    if ( !is_singular() )
        return;

    if ( current_user_can( 'moderate_comments' ) )
        return;  // moderators can see all comments

    if ( get_current_user_id() == get_queried_object()->post_author )
        return;  // the author of the post can see all comments

    $comments_query->query_vars['user_id'] = get_current_user_id();
}
if ( !is_admin() )
    add_action( 'pre_get_comments', 'restrict_visible_comments' );

Note that the above won’t display comments left by non-registered users.

Update: Nevermind, this won’t work because get_comments() isn’t used consistently in comments_template():

http://core.trac.wordpress.org/browser/tags/3.1.3/wp-includes/comment-template.php#L882

Leave a Comment