WordPress自定义查询以获取过去7天内评论最多的帖子

时间:2011-06-25 作者:rpophessagr

我正在尝试对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);
提前谢谢。

EDIT 查询已更改为某种工作方式,但我仍然没有正确获取日期范围。。。它正在返回有史以来评论最多的帖子,而不仅仅是过去一周评论最多的帖子。

2 个回复
最合适的回答,由SO网友:jackreichert 整理而成

This should do it:

$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 YEAR) 
        GROUP BY wposts.ID
        ORDER BY comment_count DESC
        LIMIT 0 ,  10
 ";
SO网友:Greg

comments表中的comment\\u count列是相关帖子上所有评论的总和。

要获取一段时间内的注释,必须对每组中的注释进行计数()。

$query="SELECT COUNT(comment_id) as num, 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 num DESC
        LIMIT 0 ,  10
 ";

结束

相关推荐