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?
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?
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