How to get all users with author permission - WordPress

In this post we will see one of the ways to consult all users with the author permission "publish_posts".

How to get all users with author permission

Listing users by their function

By default WordPress has some functions defined for registered users. They are:

  • Subscriber
  • Collaborator
  • author
  • Editor
  • Commercial

Some plugins can add new function types making WordPress panel administration even more flexible.

Let's go to the code

First we mount an array with the function we want, in our example would be "post post".

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

Now we set up the query:

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

You can also use the get_users function more directly:

get_users (array ('role__in' => array ('administrator', 'editor', 'author')));

About WP_Roles

The role option "" is simple, the structure is organized by the name of the function that stores the name in the value of the 'name' key. The resources are stored as an array in the value of the 'capacity' key.

array ('rolename' => 'array' => array ()))

Archive: wp-includes / class-wp-roles.php

Additional Function

Retrieving the Author's Role Within a Post's Loop

Add to file functions.php of your theme:

function get_author_role () {global $ authordata; $ author_roles = $ authordata-> roles; $ author_role = array_shift ($ author_roles); return $ author_role; }

Example of use:

if (have_posts ()): while (have_posts ()): the_post (); echo get_the_author (). ' | '.get_author_role (); endwhile; endif;

Rate This Article!

Click on the stars to give a note of 1 to 5.

1 star2 stars3 stars4 stars5 stars (No Ratings Yet)
Loading ...

Doubts? Make a comment below or send us a message by clicking here.

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