How to query post by user role?

The question:

I want to query posts by their author role. And do something with the post based on the role.

I know we can get the post by get_posts or WP_query, the problem is there are no argument to sort the post based on the author role. Or, we also can combine get_users and get_posts together, like this

$users = get_users(array(role => 'author'));
foreach($users as $user){
//here we can use get_posts to query the posts by the $user->ID   
} .....

this is too clumsy to doing this. I want to know if there any other way to query the posts based on the role, SQL query perhaps?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I haven’t really messed around with custom post queries before, but here is my try at a solution:

function get_posts_by_author_role($role) {
    global $wpdb;
    return $wpdb->get_results( "SELECT p.* FROM {$wpdb->posts} p, {$wpdb->usermeta} u"
                                ." WHERE    p.post_type     = 'post'"
                                ." AND      p.post_status   = 'publish'"
                                ." AND      u.user_id       = p.`post_author`"
                                ." AND      u.meta_key      = 'wp_capabilities'"
                                ." AND      u.meta_value    LIKE '%"{$role}"%'" );
}

This function will return posts only if their author has the role specified.
It is tested and working on my local install of 3.4, but let me know if you are having any problems with it.

I hope this helps.

Example Usage:

$posts = get_posts_by_author_role('author');
foreach($posts as $post) echo $post->post_title, '<br />';

Method 2

try this

create a function to alter the query’s where clause:

function authors_where_filter( $where ) {
        global $wpdb;
        $ids = get_users(array('role' => 'author' ,'fields' => 'ID'));
        $where .= " AND post_author IN ($ids)";
        return $where;
}

and then before you query just hook it ex:

add_filter('posts_where','authors_where_filter');
$all_posts = new WP_Query(array('posts_per_page' => -1 .....
remove_filter('posts_where');

and you should get all posts of author users in a single query, (well two actually one to get the users and the other is to get the posts)

Method 3

First get the user id’s by role with a function:

function author_ids_by_role() {
        $ids = get_users(array('role' => 'author' ,'fields' => 'ID'));
        return $ids;
}

Then in your query you can use this array of user id’s as the following:

$role_ids = author_ids_by_role();
    $args = array(
      "post_count"      => -1, 
      "posts_per_page"  => 10,
      "post_type"       => "my_custom_posttype",
      "author__in"      => $role_ids
    );

If you want to query the post by role of your current user. Use the following function instead:

function author_ids_by_role() {
        global $current_user;

        $user_roles = $current_user->roles;
        $user_role = array_shift($user_roles);

        $ids = get_users(array('role' => $user_role ,'fields' => 'ID'));

        return $ids;
}

Be aware that this last function only works when users got only 1 role.

Method 4

You can use Roles and Capabilitied check by deploying function:

current_user_can( $capability );

For example:

if(current_user_can('read') && !current_user_can('edit_posts')){
//code for subscribers...
}
else if(current_user_can('edit_posts') && !current_user_can('edit_pages')){
//code for authors...
}
else if(current_user_can('edit_pages') && !current_user_can('delete_themes')){
//code for editors....
}
else {
//code for admins...
}

You will have one more check if you use multi-site for super admins.

Note that you have to use two checks because without other check, some user would get multiple queryes.


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

Leave a Comment