WordPress 自定义查询过去 7 天内评论最多的帖子
我正在尝试在 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);
该查询似乎获得了所有时间评论最多的帖子,而不是过去一周评论最多的帖子。
提前致谢。
I'm trying to do a query on wpdb to get the posts commented on most in the past week... Any ideas what I'm doing wrong?
$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);
The query seems to get top commented posts of all time, instead of the top commented posts that have been commented on in the past week.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Lonut,它解决了日期范围问题和评论最多的问题,但它不会仅选择过去 X 时间间隔内评论最多的帖子评论。
这应该可以做到:
编辑:过滤日期从“年”更改为“周”
Lonut, that takes care of the date range problem and most commented, but it wont select only the most commented posts that were commented on in the past X time interval.
This should do it:
EDIT: Filter on date changed from YEAR to WEEK
这应该有效:
This should work:
您将按
posts
表的comment_count
字段进行排序。尝试根据该组匹配的评论数量进行排序:You're ordering by the
comment_count
field of theposts
table. Try ordering on the number of comments matched for the group: