10

In the Comments page (/wp-admin/edit-comments.php), every logged in user can see all the site comments.
comments page


I'd like the users to see only his/her own comments and the comments that are left to his/her posts.

How can I filter this?

9

Either of these 3 will help you:

//Before getting the comments, on the WP_Comment_Query object for each comment
add_action('pre_get_comments', 'wpse56652_filt_comm');

//Applied on the comments SQL Query, you can modify the 'Where' part of the query
add_filter('comments_clauses', 'wpse56652_filt_comm');

//After the comments are fetched, you can modify the comments array
add_filter('the_comments', 'wpse56652_filt_comm');

function wpse56652_filt_comm($param) {
    //access the current user
    global $current_user;
    get_currentuserinfo();

    //current users id = $current_user->ID;

    //Current users posts, check get_posts params to change as per your need
    $user_posts = get_posts(array('author' => $current_user->ID, 'posts_per_page' => -1));

    echo '<pre>';
    print_r($param);
    echo '</pre>';

    return $param;
}

Additionally you can use global $pagenow to make sure that the code runs only on this page.

Sorry I'm kinda unwell today, so couldn't write down an example! ;)

Edit:

/**
 * Show only the Comments MADE BY the current logged user
 * and the Comments MADE TO his/hers posts.
 * Runs only for the Author role.
 */

add_filter('the_comments', 'wpse56652_filter_comments');

function wpse56652_filter_comments($comments){
    global $pagenow;
    global $user_ID;
    get_currentuserinfo();
    if($pagenow == 'edit-comments.php' && current_user_can('author')){
        foreach($comments as $i => $comment){
            $the_post = get_post($comment->comment_post_ID);
            if($comment->user_id != $user_ID  && $the_post->post_author != $user_ID)
                unset($comments[$i]);
        }
    }
    return $comments;
}
  • thank you for answer - just few hour ago i found article for solving such problem for posts in your blog! I even find params for comments but i don't know ho to set current logged user id. if i want just to show only his comments i can use his ID but i also want to show comments to his posts. How it can be done? – moonvader Jun 27 '12 at 11:51
  • You're welcome! Check the answer now, I've updated it. – Rutwick Gangurde Jun 27 '12 at 11:57
  • now it is showing all comment params in wp-admin/edit-comments.php page - but i still can see all comments( – moonvader Jun 27 '12 at 12:02
  • That's because you have to filter the comments! I've put that print_r for testing sake! – Rutwick Gangurde Jun 27 '12 at 12:07
  • this filtering have to be made inside wpse56652_filt_comm function? can you show me an example how to show only comments from user with id=4? – moonvader Jun 27 '12 at 12:12

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