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

发布于 2024-11-17 00:53:35 字数 499 浏览 3 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱倦旧时光 2024-11-24 00:53:35

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
 ";

编辑:过滤日期从“年”更改为“周”

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:

$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
 ";

EDIT: Filter on date changed from YEAR to WEEK

只涨不跌 2024-11-24 00:53:35

这应该有效:

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");

This should work:

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");
自由如风 2024-11-24 00:53:35

您将按 posts 表的 comment_count 字段进行排序。尝试根据该组匹配的评论数量进行排序:

ORDER BY count(*) desc

You're ordering by the comment_count field of the posts table. Try ordering on the number of comments matched for the group:

ORDER BY count(*) desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文