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?


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 />';

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:

$all_posts = new WP_Query(array('posts_per_page' => -1 .....

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)


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.


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.

  • the problem is the function is made to run in behind, not depend to current logged in user. And I prefer to use the role's name to run the check,because I have multiple custom user role which created by role management plugin.
    – dev-jim
    Jun 21 '12 at 16:24
  • Maybe you can use this function then that will return user role as a string, and then check it with if's function get_current_user_role() { global $wp_roles; $current_user = wp_get_current_user(); $roles = $current_user->roles; $role = array_shift($roles); return isset($wp_roles->role_names[$role]) ? translate_user_role($wp_roles->role_names[$role] ) : false; } Jun 21 '12 at 16:28
  • 1
    this only solve part of my question, the next thing is to query the posts by the role. Your codes will be clumsy as I did. Which is not what I wanted.
    – dev-jim
    Jun 21 '12 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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