wordpressrole

How to retrieve email addressess of users with specific role in WordPress through this code


I am able to retrieve email addresses of all the users in WordPress with the help of this code:

<?php
$users = get_users();
foreach ($users as $user ) { 
$email = get_user_meta($user->ID, "email", true);
?>

I would like to retrieve the email addresses of users only with Subscriber role (slug = subscriber) by modifying the above code.

Please advice.


Solution

  • get_users() allows you to query by a specific role:

    $users = get_users( array( 'role' => 'subscriber' ) );
    foreach ( $users as $user ) { 
       $email = get_user_meta($user->ID, "email", true);
       echo $email;
    }