2

我正在尝试对 wpdb 进行查询,以获取过去一周中评论最多的帖子...任何想法我做错了什么?

$querystr = "SELECT comment_count, ID, post_title
        FROM $wpdb->posts wposts, $wpdb->comments wcomments
        WHERE wposts.ID = wcomments.comment_post_ID
        AND wcomments.comment_date >= CURDATE() - 7 
        GROUP BY wposts.ID
        ORDER BY comment_count DESC
        LIMIT 0 ,  10
 ";

 $pageposts = $wpdb->get_results($querystr);

该查询似乎获得了有史以来评论最多的帖子,而不是过去一周评论最多的帖子。

提前致谢。

4

3 回答 3

2

这应该有效:

global $wpdb;
$querystr = $wpdb->get_results("SELECT comment_count, ID,post_title 
    FROM $wpdb->posts 
    WHERE 
    post_status='publish' AND 
    post_date > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 WEEK) 
    ORDER BY comment_count DESC 
    LIMIT 0 , 10");
于 2011-06-25T09:53:56.650 回答
2

Lonut,负责处理日期范围问题和评论最多,但它不会选择在过去 X 时间间隔内评论最多的帖子。

这应该这样做:

$querystr = "SELECT comment_count, ID, post_title
        FROM $wpdb->posts wposts, $wpdb->comments wcomments
        WHERE wposts.ID = wcomments.comment_post_ID
        AND wposts.post_status='publish'
        AND wcomments.comment_approved='1'
        AND wcomments.comment_date > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 WEEK) 
        GROUP BY wposts.ID
        ORDER BY comment_count DESC
        LIMIT 0 ,  10
 ";

编辑:过滤日期从 YEAR 更改为 WEEK

于 2011-06-25T10:29:32.693 回答
1

您按表格的comment_count字段排序。posts尝试按组匹配的评论数量排序:

ORDER BY count(*) desc
于 2011-06-25T09:53:59.487 回答