List custom wordpress user meta based on id

I am trying to get custom meta data from all users. I have the following code the returns no errors. So I don’t know what is going wrong. Is there a more simple way to loop over all id’s and return custom meta data?

global $wpdb;
$wp_user_search = $wpdb->get_results("SELECT ID FROM $wpdb->users ORDER BY ID");

foreach ($wp_user_search as $userid) {
    $all_meta_for_user = get_user_meta($userid->id);

    $email_alert = $all_meta_for_user['email_sms'][0];

    echo $email_alert;
}

Related posts

Leave a Reply

1 comment

  1. Use regular WordPress functions.

    In this case, get_users().

    Like this:

    $wp_user_search = get_users( 'blog_id=1&orderby=id' );
    
    foreach ( $wp_user_search as $userid ) 
    {
        $all_meta_for_user = get_user_meta($userid->ID);
        $email_alert = $all_meta_for_user['email_sms'][0];
        echo $email_alert;
    }
    

    Also note that you were using $userid->id, instead of $userid->ID.

    For checking the values of your variables use:

    • var_dump($userid)
    • or echo '<pre>' . print_r( $all_meta_for_user, true ) . '</pre>';