列出最近30天内发帖最多的用户

时间:2014-02-21 作者:Ahmed Saad

我正在尝试创建一个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");

1 个回复
最合适的回答,由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>\';
    }
?>

结束

相关推荐

由于与WPDB类的外部数据库连接,WordPress页面未发布

我正在开发wordpress plugin 它连接到外部数据库,获取数据,并将其显示在页面/帖子编辑器下方的metabox上。我使用了以下代码:<?php /** MySQL hostname */ define(\'EXT_DB_HOST\', \'localhost\'); /** MySQL database name */ define(\'EXT_DB_NAME\', \'trader\'); /** MySQL database