0

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?

1 Answer 1

0

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().

3
  • 1
    Thank you so much. - that did it perfectly! Also I really appreciate the detailed explanations - I will be using all of it, probably in this project. Thanks again @Sally.
    – Ben Blue
    Commented Jan 21, 2022 at 15:29
  • PS: I revised my answer to correct a typo in the "magic __get() method" part. But actually, I added that part just so that you know; hence you should just manually call get_user_meta(), which is why I said, "alternatively". :)
    – Sally CJ
    Commented Jan 23, 2022 at 13:33
  • 1
    Excellent, thank you for the detailed clarification!
    – Ben Blue
    Commented Jan 24, 2022 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.