Wordpress - how to get list of all users and their metadata

I think you should use wp-api functions which will do everything for you.

  • get_users() function will get all users data just define fields which u require.
  • get_user_meta() function will get usermeta data.
 
$users = get_users( array( 'fields' => array( 'ID' ) ) );
foreach($users as $user){
        print_r(get_user_meta ( $user->ID));
    }

At bit more formal way to achieve the same results are as follows Substitute any role as necessary and this code assumes you are using woocommerce for more detailed information.

function GetSubscriberUserData()
{
$DBRecord = array();
$args = array(
    'role'    => 'Subscriber',
    'orderby' => 'last_name',
    'order'   => 'ASC'
);
$users = get_users( $args );
$i=0;
foreach ( $users as $user )
  {
  $DBRecord[$i]['role']           = "Subscriber";   
  $DBRecord[$i]['WPId']           = $user->ID;  
  $DBRecord[$i]['FirstName']      = $user->first_name;
  $DBRecord[$i]['LastName']       = $user->last_name;
  $DBRecord[$i]['RegisteredDate'] = $user->user_registered;
  $DBRecord[$i]['Email']          = $user->user_email;

  $UserData                      = get_user_meta( $user->ID );  
  $DBRecord[$i]['Company']        = $UserData['billing_company'][0];    
  $DBRecord[$i]['Address']        = $UserData['billing_address_1'][0];
  $DBRecord[$i]['City']           = $UserData['billing_city'][0];
  $DBRecord[$i]['State']          = $UserData['billing_state'][0];
  $DBRecord[$i]['PostCode']       = $UserData['billing_postcode'][0];
  $DBRecord[$i]['Country']        = $UserData['billing_country'][0];
  $DBRecord[$i]['Phone']          = $UserData['billing_phone'][0];
  i++;
  }
return DBRecord;
}