WordPress - 按自定义字段计算用户数



我为我的wordpress用户创建了一个自定义字段:包含男性和女性的性别。

现在我想计算一下男性和女性用户的数量。

最好使用函数.php

此代码计算用户总数。可以扩展到计算性别吗?

add_shortcode( 'ucount', 'wpsites_user_count' ); //Count users
function wpsites_user_count() {
$count = count_users();
echo "There are " , $count['total_users'] , " on your website!";} 

这完成了这项工作:

add_shortcode( 'countfemale', 'wpsites_user_countfemale' ); //Count users
function wpsites_user_countfemale() {
$args  = array(
'meta_key' => 'gender', //any custom field name
'meta_value' => 'female' //the value to compare against
);
$users= new WP_User_Query( $args );
echo $users->get_total();
} 

最新更新