I want to list in a drop-down box all users who have role "author"
How can i do it?
Thank you
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityyou can create a function which gives you all users which have role author in a drop down list.
Note: It is a dummy example modify it as you want( put this function in functions.php
or custom plugin
).
Use: Call this function any where in theme like <?php ravs_author_dropdown_list(); ?>
function ravs_author_dropdown_list() {
// query array
$args = array(
'role' => 'author'
);
$users = get_users($args);
if( empty($users) )
return;
echo'<select>';
foreach( $users as $user ){
echo '<option value="'.$user->data->user_login.'">'.$user->data->display_name.'</option>';
}
echo'</select>';
}
For posterity, adding a response since as the accepted answer uses a custom method yet WordPress supports this in the wp_dropdown_users
method:
Use:
wp_dropdown_users(['who' => 'authors']);
To query other roles [though not asked for in this question], you can use the role
parameter, introduced in WordPress 4.7.0:
wp_dropdown_users(['role' => 'authors']);
More here: https://developer.wordpress.org/reference/functions/wp_dropdown_users/
Use WordPress' built-in wp_dropdown_users
with an advanced query as discussed in https://stackoverflow.com/questions/34142789/change-wp-dropdown-users-filter-by-user-role