Wpdb get_Results()仅返回2行

时间:2016-01-13 作者:Dawid Adach

我正在尝试创建自定义管理小部件,以显示过去几天内注册的用户数。我创建了如下函数:

function mdbootstrap_add_dashboard_recent_users(){ 
global $wpdb;

$query = "
    SELECT count(*) as counter, DATE(wp_users.user_registered) as regdate
    FROM wp_users 
    GROUP BY DATE(wp_users.user_registered) 
    ORDER BY DATE(wp_users.user_registered) desc";

$posts = $wpdb->get_results($query,OBJECT);
print_r( $posts);
 }
但是,由于某些原因,它只返回2行:

Array ( [0] => stdClass Object ( [counter] => 1 [regdate] => 2016-01-12 ) [1] => stdClass Object ( [counter] => 3 [regdate] => 2016-01-04 ) )
哪个BTW显示了错误的值,因为从PHP MyAdmin运行的同一个查询给出了如下结果:

柜台登记日期2016-01-13 26 2016-01-08 24 2016-01-07 24 2016-01-06 18 2016-01-05 01-03我一直在寻找类似的问题,但大多数主题只涉及人们使用get_row 而不是get_results. 我玩过不同的参数,比如ARRAY_N, OBJECT, ARRAY_A 但不起作用-查询始终返回2行。

1 个回复
SO网友:Mayur Chauhan

您可以尝试以下查询

SELECT (
    SELECT count(*) 
    FROM wp_users as temp 
    WHERE DATE(temp.user_registered) = DATE(wp_users.user_registered)
    ) as counter, 
wp_users.*
FROM wp_users