I need to fetch the list of all users who are Authors (with Author capabilities)

get_users allows to fetch users with roles, but if users are filter with author role, it skips users with role admin and editor (but they can also create posts).

So I need some other way to fetch all users having capability of Author Role (they can be from Administator, Editor or any other custom roles with Authors capability)

To Be more precise – Need all users with “publish_posts” capability.

4 Answers
4

Here’s a way to collect roles with the publish_posts capability:

$roles__in = [];
foreach( wp_roles()->roles as $role_slug => $role )
{
    if( ! empty( $role['capabilities']['publish_posts'] ) )
        $roles__in[] = $role_slug;
}

and then we can query users with these roles:

if( $roles__in )
    $users = get_users( [ 'roles__in' => $roles__in, 'fields' => 'ids' ] );

where we might need pagination for large amount of users.

One can also loop over few users and check with:

user_can( $user, 'publish_posts' )

See docs here.

Leave a Reply

Your email address will not be published. Required fields are marked *