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?
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?
Share Improve this question edited Jan 20, 2022 at 20:41 Ben Blue asked Jan 20, 2022 at 17:28 Ben BlueBen Blue 1171 silver badge8 bronze badges1 Answer
Reset to default 0I 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 useWP_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 thenumber
argument), then you'd usecount( $users )
in place of the$user_query->get_total()
.