If you’re trying to get users with all roles (in your example, administrator
and author
), your code looks like it should work.
If you want users with any role (administrator
or author
, you’ll need to split the string in the role
attribute into an array using explode()
, and use the role__in
array.
Something like this should work:
function get_emails_by_multiple_roles ( $atts ) {
$atts = shortcode_atts( array('role' => '' ), $atts );
$roles = explode( ',', $atts[ 'role' ] );
// Trim whitespace from the role names.
foreach ( $roles as $key => $role ) {
$roles[ $key ] = trim( $role );
}
$user_args = array ( 'role__in' => $roles );
$users = get_users( $user_args );
foreach ( $users as $user ) :
$role_emails[] = $user->user_email;
endforeach;
$all_role_emails = implode(', ', $role_emails);
return $all_role_emails;
}
add_shortcode('multiple_roles_emails', 'get_emails_by_multiple_roles');