我正在尝试创建一个MySQL查询,以显示过去30天内帖子数量最多的用户的降序列表。
这与我无关:
$where .= " AND post_date > \'" . date(\'Y-m-d\', strtotime(\'-30 days\')) . "\'";
$top_users = $wpdb->get_results("SELECT COUNT(ID) AS post_count, post_author, post_date, user_id FROM $wpdb->posts, $wpdb->usermeta WHERE $wpdb->posts.post_author = $wpdb->usermeta.user_id AND post_type = \'post\' AND post_status = \'publish\' $where");
最合适的回答,由SO网友:Sudeep K Rana 整理而成
这是你需要的。
<?php
$date = new DateTime(\'NOW\');
$date->sub(new DateInterval(\'P30D\')); //30 Days Interval
echo $back30days= $date->format(\'Y-m-d H:i:s\') . "\\n";
global $wpdp;
$top_users = $wpdb->get_results("select count(users.user_nicename) as posts, users.user_nicename as user_name from $wpdb->users as users join $wpdb->posts as posts where users.ID = posts.post_author AND posts.post_status=\'publish\' AND posts.post_date_gmt > \'$back30days\' GROUP BY users.user_nicename ORDER BY count(users.user_nicename) DESC");
echo \'<h3>In Past 30 Days</h3>\';
foreach ($top_users as $top_user){
echo \'<p>\'.$top_user->user_name.\' has written \'.$top_user->posts.\' posts</p>\';
}
?>