5

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?

4

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

  • Can you please post some more details. – AgentWP Jun 18 '11 at 13:01
  • I would like some more info about this too – EnexoOnoma Jun 19 '11 at 1:20
  • Updated answer. – scribu Jun 19 '11 at 9:46
  • @scribu Thanks for the code. It works great except that it doesn't fulfill this condition, "when a commentator visits the post, he should be able to see 'only' his own comment(s)" assuming that he is not post author or admin. Can you please help? And also all users are registered, so that is not a problem. – AgentWP Jun 19 '11 at 10:38
  • I am using Members plugin by Justin Tadlock if it has something to do with the $comments_query->query_vars['user_id'] = get_current_user_id(); line not working. – AgentWP Jun 19 '11 at 10:48
0

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;
}
?>
  • Because $comments are actual array of comments, I think you should not use raw SQL queries to get them again. Instead of that, you can loop the $comments and unset() not matched comments. – Anh Tran Aug 1 '11 at 9:46
0

@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!

0

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;
}
-1

Useful Comments Plugin..If you are not well known about codings means Plugin is the Easiest Way to Do that.:) I hope you got answer...:)

  • None of the plugin is useful there. – AgentWP Jun 18 '11 at 13:01
  • thanks for your negative vote:).Really it's help to Increase Answering the questions... – Ramkumar M Jun 21 '11 at 11:32
  • I did not vote for you man! – AgentWP Jun 21 '11 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.