Comment visibility

There will be many people commenting on a post. So, when a commentator visits the post, he should be able to see ‘only’ his own comment(s).Post author can see all comments only on his own post.

How can I do this in WordPress?

Related posts

Leave a Reply

5 comments

  1. 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

  2. use the comments_array filter, and remove the unnecessary comments.

    Some loose code to get you started

    <?php
    add_filter('comments_array','display_only_user_comments')
    function display_only_user_comments($comments){
        $comemnts=NULL;
        /*modify this part to get only user comments*/
        if ( $user_ID) {
                  $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post->ID, $user_ID));
            } else if ( empty($comment_author) ) {
                    $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
           } else {
                    $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email));
    
        }
    
        return $comments;
    }
    ?>
    
  3. @Satish… Thanks a lot man! I don’t know if you’re solution worked for the question asker, but it worked for me! I has spent 2 days trying things with ‘comments_clauses’ and ‘pre_get_comments’, and had given up!

  4. My solution for author to see only his comments

    add_filter('pre_get_comments','display_only_user_comments');
    function display_only_user_comments($query){
        global $pagenow;
        if('edit-comments.php' != $pagenow || $query->is_admin)
            return $query;
    
        if( !current_user_can( 'manage_options' ) ) {
            global $user_ID;
            $query->query_vars['post_author'] = $user_ID;
        }
        return $query;
    }