admin管理员组文章数量:1291609
Users can choose a value from a multiple choice checkbox on sign up. In this example the choices are Cat or Dog.
I have a WP User Query and want to count the amount of users that have each meta value, and display something like '45 Cat People, 15 Dog People'.
Below are two separate queries but I'd like to loop through the values and display using one foreach query but not sure how to access the meta value to do this. Also the ability to output the value name rather than hard coding it would be useful...
<?php
// Meta value 'Cats'
$user_query = new WP_User_Query(array(
'role' => 'Subscriber',
'meta_query' => array(
array(
'key' => 'favourite_animal',
'value' => 'Cat',
),
)
));
$users = $user_query->get_results();
$total = $user_query->get_total();
echo $total . 'Cat people';
// Meta value 'Dogs'
$user_query = new WP_User_Query(array(
'role' => 'Subscriber',
'meta_query' => array(
array(
'key' => 'favourite_animal',
'value' => 'Dog',
),
)
));
$users = $user_query->get_results();
$total = $user_query->get_total();
echo $total . 'Dog people';
?>
本文标签: loopQuery users and count totals based on meta values
版权声明:本文标题:loop - Query users and count totals based on meta values 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741537692a2384127.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论