How do I echo all users' display_name and their meta_value who have a certain meta_key?

I have a meta_key inside the table usermeta called 'sms_subscriber'. The meta_value for it is their phone number. I want to echo a list of all users who have that field; in other words, a list of each user's name, phone number, and the quantity.

The page output might look something like this:

SMS Subscribers (3)
1 ben blue +15553335555
2 bob bananas +445557778888
3 jerry johnson +13334445555

I have tried many different queries and have successfully echo an array of users' display_name, and a list of meta_value with meta_key equal to sms_subscriber, but not in the same query. I've also got a query in Sequel Pro which outputs the sum.

Here are those:

Display the correct list of user's display names:

$user_query = new WP_User_Query( array( 'meta_key' = 'sms_subscriber' ) );
$users = $user_query-get_results();
if (!empty($users)) {
    echo 'ul';
    foreach ($users as $user){
        echo ' li' . $user-display_name . $user-meta_value . '/li';
    } 
    echo '/ul';
}

Display the correct list of phone numbers:

$phones = $wpdb-get_col(SELECT meta_value FROM $wpdb-usermeta WHERE meta_key = 'sms_subscriber' );
$phones = array_keys(array_flip($phones));
// var_dump( $phones );
foreach ($phones as $phone) {
    echo $phone . 'br';
}

In Sequel Pro's query window, displays the sum:

select sum(um.meta_key = 'sms_subscriber'), 
from wp_usermeta um
join wp_users u on u.id = um.user_id
where um.meta_key = 'sms_subscriber'

Can you help me get these three queries working as one, so i can create the list?

Topic wp-user-query meta-query meta-value user-meta mysql Wordpress

Category Web


I want to echo a list of all users who have that field; in other words, a list of each user's name, phone number, and the quantity.

Try this, which is based on your 1st snippet:

( and this is all you need; no need for the $wpdb->get_col() snippet )

$user_query = new WP_User_Query( array( 'meta_key' => 'sms_subscriber' ) );
$users = $user_query->get_results();
if (!empty($users)) {
    echo '<h3>SMS Subscribers (' . $user_query->get_total() . ')</h3>';

    echo '<ul>';
    foreach ($users as $user){
        // this one uses the magic __get() method in WP_User, i.e. $user-><meta key>
//      echo ' <li>' . $user->display_name . " $user->sms_subscriber" . '</li>';

        $sms_subscriber = get_user_meta( $user->ID, 'sms_subscriber', true );
        echo ' <li>' . $user->display_name . " $sms_subscriber" . '</li>';
    }
    echo '</ul>';
}

Things to note:

  • You can use get_user_meta() to retrieve the value of a user meta.

    Alternatively, WP_User has a magic __get() method which you can use to get a meta value without having to use the above function, but only to get a single meta value (regardless if that value is a string/text, number, array, etc.). So for example, you could use $user->sms_subscriber like you could see in the code I commented out above.

  • To get the "quantity" or sum as you said it, which is the total number of users that matched your query arguments, you can use WP_User_Query::get_total().

    If you just wanted to count the number of items in $users (i.e. the 1st page of the query's results — the results can be paginated using the number argument), then you'd use count( $users ) in place of the $user_query->get_total().

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.