I am trying to output a username and user email outside of a foreach loop. I am trying to send an email to all WordPress users that are within a certain User Role.

Here is my code:

// Get users and their roles
    $user_args = array(
      'role__in' => 'new_role', 
      'orderby'  => 'user_nicename',
      'order'    => 'ASC'
    );  
    $users = get_users($user_args); 

    foreach ( $users as $user ) :       
        $user_name  = $user->user_email;
        $user_email = $user->display_name;          
    endforeach;

So I can then use them in other areas of the page, ie:.

echo 'Hello, ' . $user_name;
echo 'Send to: ' .$user_email;

I’ve printed out the $users array which contains the following:

Array
(
    [0] => WP_User Object
        (
            [data] => stdClass Object
                (
                    [ID] => 46
                    [user_login] => huw
                    [user_pass] => u7CqxbuQDvApUAF6tT.
                    [user_nicename] => huw
                    [user_email] => huw@xxxx.co.uk
                    [user_url] => 
                    [user_registered] => 2017-02-06 11:13:09
                    [user_activation_key] => 1486379590:$P$BkisA4T5j1S/ZjRageafNYHfsdin1S0
                    [user_status] => 0
                    [display_name] => Huw Daniel Rowlands
                )

            [ID] => 46
            [caps] => Array
                (
                    [sssg] => 1
                    [new_role] => 1
                    [site_member] => 1
                    [test_role] => 1
                )

            [cap_key] => jciw_capabilities
            [roles] => Array
                (
                    [0] => sssg
                    [1] => new_role
                    [2] => site_member
                    [3] => test_role
                )

            [allcaps] => Array
                (
                    [read] => 1
                    [sssg] => 1
                    [new_role] => 1
                    [site_member] => 1
                    [test_role] => 1
                )

            [filter] => 
        )

    [1] => WP_User Object
        (
            [data] => stdClass Object
                (
                    [ID] => 308
                    [user_login] => jeremy
                    [user_pass] => LLOKbkPOWQsBKUIk2qL1
                    [user_nicename] => magnus
                    [user_email] => jeremy@gmail.com
                    [user_url] => 
                    [user_registered] => 2017-05-03 19:24:42
                    [user_activation_key] => 1493839482:$P$BI/IYldCzsXZowLEiNfxiUkIwVdDKV0
                    [user_status] => 0
                    [display_name] => Jeremy
                )

            [ID] => 308
            [caps] => Array
                (
                    [new_role] => 1
                    [sssg] => 1
                    [site_member] => 1
                )

            [cap_key] => jciw_capabilities
            [roles] => Array
                (
                    [0] => new_role
                    [1] => sssg
                    [2] => site_member
                )

            [allcaps] => Array
                (
                    [read] => 1
                    [new_role] => 1
                    [sssg] => 1
                    [site_member] => 1
                )

            [filter] => 
        )

)

Read more here: Using a variable outside PHP Foreach Loop


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: