我一直在到处寻找一种方法来计算get\\u用户查询的结果量。
我发现的大部分内容都是统计帖子查询中的帖子总数,而不是统计get\\u users查询中的用户总数。
有人能给我指出吗?非常感谢。
我一直在到处寻找一种方法来计算get\\u用户查询的结果量。
我发现的大部分内容都是统计帖子查询中的帖子总数,而不是统计get\\u users查询中的用户总数。
有人能给我指出吗?非常感谢。
当您使用get_users()
它检索符合中给定条件的用户数组$args
这意味着您可以简单地使用PHPcount()
功能,例如:
$users = get_users($args);
$number_of_users = count($users);
Bainternet的答案为您提供了一个查询的用户数,如果您需要多个页面的用户,它也没有帮助。
如果你有成千上万的用户,那不是你想要的。您可以使用WP\\u User\\u Query,就像您对帖子使用WP\\u Query一样。这与get\\u用户非常相似——参数相同,但特性和用法不同。
最重要的是你可以the total number of users who match your criteria 没有实际返回所有结果(并且可能达到内存限制)。
Usage
$user_query = new WP_User_Query(array(
\'number\' => 15,
\'paged\' => 1
));
$users = $user_query->get_results(); // array of WP_User objects, like get_users
$total = $user_query->get_total(); // int, total number of users (not just the first page)
Examples:
echo $users[0]->get(\'display_name\');
// "Radley Sustaire"
echo count($users);
// "15" (count from the first page of users)
echo $user_query->get_total();
// "86" (count from all pages of users)
$num_pages = ceil( $user_query->get_total() / 15 );
// "6" (number of pages of users. 15 is the "users per page" from the first example)
// (* do NOT use count($users) in place of 15. try a separate $per_page variable)
我是如何实现这一点的:
function getUsers($args = array(), $count = false)
{
// Default behaviour is to return full user information
$method = \'get_results\';
// If count is true then change the WP_User_Query method
// and limit the fields SELECT\'d to \'ID\' to speed up the query
if($count === true) {
$method = \'get_total\';
$args[\'fields\'] = \'ID\';
}
$userQuery = new WP_User_Query($userArgs);
return call_user_func(array($userQuery, $method));
}
用法:$args = array(\'role\' => author,
\'meta_key\' => \'foo\',
\'meta_value\' => \'bar\'
);
// Returns (array) WP_User objects (a la get_users())
$users = getUsers($args);
// Returns (int) number of of users matched by $args
$count = getUsers($args, true);
除了比$users = get_users($args); $count = count($users);
它确保如果您只需要总数,那么它将从DB获取实现此目标所需的最小值。尝试以下函数以获取数组中所有用户的计数,以及基于用户角色的用户计数:
count_users();
输出如下:Array (
[total_users] => 32
[avail_roles] => Array (
[administrator] => 1
[subscriber] => 28
[sub_administrator] => 1
[editor] => 2
)
)
可以找到更多信息here.我与我管理的一个网站发生jquery冲突。引用此链接:(http://www.terraworldltd.com/pc/business/2056.html ), 产品页面由奇特的缩放组成,当点击图片时,图片会缩小。对jquery的调用是在单节点上完成的。产品的php。代码如下:<?php get_header(); ?> <script> var closebutton=\'<?php bloginfo(\'template_directory\'); ?