I want the comments to an author's posts ONLY viewable by the post author and the commentor.
For example the author publishes a post, commentor A posts a comment on the post and the post author post's a reply comment to commentor A. ONLY the post author and commentor A can view their comments. If commentor B does the same then only the post author and commentor B can view their comments.
Essentially, the post author can see all the comments but the public can only see their own comments, and the post author's comments if the post author has replied.
Thanks and regards,
Pete
Bob answers:
Please try by adding this code in your theme's functions.php file
Note your comments must be approved to be visible.
function restrict_comments( $comments , $post_id ){
global $post;
$user = wp_get_current_user();
if($post->post_author == $user->ID){
return $comments;
}
foreach($comments as $comment){
if( $comment->user_id == $user->ID || $post->post_author == $comment->user_id ){
if($post->post_author == $comment->user_id){
if($comment->comment_parent > 0){
$parent_comm = get_comment( $comment->comment_parent );
if( $parent_comm->user_id == $user->ID ){
$new_comments_array[] = $comment;
}
}else{
$new_comments_array[] = $comment;
}
}else{
$new_comments_array[] = $comment;
}
}
}
return $new_comments_array; }
add_filter( 'comments_array' , 'restrict_comments' , 10, 2 );
pjeaje comments:
I think you've nailed it Bob! I'll test it a bit more and award you the money soon. Thanks for your help.
This would make an awesome plugin in the repository too by the way!
Dbranes answers:
Hi, @Kyle can explain this better than I do,
but let me try a little bit different and untested approach using the <em>comments_array</em> filter:
/**
* An untested attempt to filter comments, so that the post author sees all
* and current user sees only his comments and the post author's replies.
*
* @see http://www.wpquestions.com/question/showChronoLoggedIn/id/10125
*/
add_filter( 'comments_array', 'wpq_comments_filter' );
function wpq_comments_filter( $comments ) {
$author_id = $GLOBALS['post']->post_author;
$cuid = get_current_user_id();
$lookup = array();
foreach( $comments as $comment ) {
$lookup[$comment->ID] = $comment;
}
$newcomments = array();
foreach( $comments as $comment ) {
// user is logged in:
if( $cuid > 0 ) {
$parent_id = $lookup[$comment->ID]->comment_parent;
if(
// The user sees only his own comment:
$author_id != $cuid && $comment->user_id == $cuid
// The user got a reply from the author that should be visible:
|| $author_id == $comment->user_id && $parent_id > 0 && $lookup[$parent_id]->user_id == $cuid
// The user is the post author, so everything is visible:
|| $author_id == $cuid
) {
$newcomments[] = $comment;
}
}
}
return $newcomments;
}
You might have to adjust this further to your needs ...
<em>Update</em>: It looks like @Bob beat me to it ;-)
ps: But I prefer a simple array lookup instead of calling <em>get_comment</em> for each comment.
Kyle answers:
Part of this depends on your theme/framework. You're going to need to edit either an associated template where you are outputting the code that the has the comment_list() function, or reformat the comments.php template itself that is being called.
Where ever that code is, you'll want to run a simple conditional:
global $post;
$user = wp_get_current_user();
if( $post->post_author == $user->ID){
//Display comment code
}
For the second part (commenter view) you can add a second conditional that goes down to the comment level. So remove the above, but then inside the comment loop run something like
while->have_posts(){
the_post();
global $post;
$author = get_comment_author();
$user = wp_get_current_user();
if($user->ID == $author){
//display comment
}
}
This is all on the fly, you'll have to do some fine tuning to your specs, theme, and finer points.
pjeaje comments:
Is there ANY way it can be done regardless of the theme i'm using?
Kyle comments:
Higher up. You will need a pre_get_posts filter that checks for the post_type and author/commenter against the current user in the same way as above, check the query_vars object and only return if both pass your requirements.
That said - pre_get_posts modifiers can be dangerous, so do thorough testing. You want to be sure you aren't affecting the values anywhere else on your site.
pjeaje comments:
I have no idea what you mean :)
I really need a dummies version/explanation.