Site icon Hip-Hop Website Design and Development

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?