Support » Fixing WordPress » Get comments by user role

  • Resolved kristina87



    I’ve searched for the past 3-4 hours for a way to solve this & no luck.

    Would any one have any idea how to display comments to a specific posts by the user role?

    Any tips would be greatly appreciated.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hello Kristina,

    My initial thoughts on the matter are to run a user query for users of the role/roles you want, stick all of their user ids in an array, and then run get_comments() with the author__in parameter.

    If you want it to apply to a specific post, then you’ll need some way of specifying which roles you want to filter for, and then you’ll need to pass that to the aforementioned user query in your template.

    I hope this is helpful. I’m about to go to bed, otherwise I would put together a working example. Hopefully I’ll have some time tomorrow.

    Hi Kendall,

    Thank you for responding – though I must admit I’m not 100% clear here.

    Ideally I would have a dropdown menu near the comments (under a specific post) that would allow to only view comments by a specific user role.

    These users will be limited in number so I’m ok with just sorting by a group of specific authors

    Thanks again,

    Sorry for going straight for a technical answer. I sometimes get ahead of myself.

    Anyway, this looks like it would require a little bit of work and might actually make for a nice plugin. Let me go ahead and say that I don’t think I’ll have time to write the plugin today, but if someone else wants to go for it, then be my guest.

    Here’s how I would build it:

    1. Get a list of user roles in the site.
    2. Use the action/filter API to insert a dropdown or checkboxes for those roles above the comments.
    3. Have a button that reloads the page with some POST data associated with each selected role.
    4. Iterate through the comments and get comment author IDs.
    5. If the author belongs to a user role specified in the POST data, add their ID to an array.
    6. Use the aforementioned author__in argument along with the comments_array filter to only show comments from authors of the specified roles.

    There is probably a few other ways of putting it together, but the above should work.

    @kristina: I know this isn’t immediately helpful, but it does appear that what you need is possible. Someone just needs to build it first. If no one else steps up, I’ll try and sink a couple of hours into it over the weekend as this could potentially be useful for a lot of people.


    Thanks again. I was far from expecting a solution straight off top, rather a glimpse of hope/some guidance for a start 🙂

    Yes, I agree that this would be a very useful plug in.

    What I have in my situation is a community of a more general public and then professionals in the field – so it is crucial to give the professionals the ability to view comments by the colleagues as the general ones will be much more numerous. I imagine other people would have similar situations.

    I found a plug in to get comments by author
    So I was thinking I might need to manually add all the authors within the relevant role & find a way to sort for that in a drop down menu. For a temporary solution

    That could work as a shorter term solution. One worrisome thing is that plugin is quite outdated so in addition to hacking it to suit your intended functionality it would need to be audited for compatibility with newer versions of WordPress. If you try it, I recommend setting up a clean WordPress instance to do your work in as you wouldn’t want to temporarily break a production site.

    I’ll try to make some headway on the plugin this weekend if nobody else is able to.


    Yes, the plugin already failed unfortunately.

    I very much appreciate your willingness to help!

    Okay. I had a little bit of time this evening. Try this:

    Plugin Name: Comment Roles
    Description: Allows filering of comments by user role.
    Version: 0.0.1
    Author: Kendall Weaver
    Author URI:
    License: GPL2
    License URI:
    function comment_roles_form() {
        global $wp_roles;
        $roles = $wp_roles->roles;
        echo '<form method="get">';
        foreach($roles as $key => $value) {
            echo '<input type="checkbox" name="comment-role[]" value="' . $key . '" />' . $value['name'] . '<br />';
        echo '<input type="submit" value="Filter">';
        echo '</form>';
    add_action( 'comments_template', 'comment_roles_form' );
    function comment_roles_filter($comments) {
        $roles = $_GET["comment-role"];
        if ($roles != NULL) {
            $users = array();
            foreach($roles as $role) {
                $userlist = get_users('role=' . $role);
                foreach($userlist as $user) {
                    $users[] = $user->ID;
            foreach($comments as $comment => $data) {
                if (!in_array($data->user_id, $users)) {
                    unset( $comments[$comment]);
        return $comments;
    add_filter( 'comments_array', 'comment_roles_filter' );


    It works!

    I wasn’t even sure that it was possible!

    I don’t think I can thank you enough. THANK YOU!!

    Glad I could help. Be sure to mark this topic as “Resolved”.

    Also the plugin will be available in the plugin repository very soon. It was approved a couple of hours ago but I probably won’t have time to set up Subversion and get everything lined up until tonight or tomorrow night.


    Thank you!



    Just a heads up that the plugin is now live in the repo:


Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Get comments by user role’ is closed to new replies.