List custom wordpress user meta based on id

2019-08-09 04:07发布

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;
}

标签: php wordpress
1条回答
ら.Afraid
2楼-- · 2019-08-09 04:59

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>';
查看更多
登录 后发表回答