Filtering the Admin Comments List to Show Only Comments from the Current User?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x