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

时间:2011-06-25 09:44:25

标签: sql wordpress jquery-post

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

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

提前致谢。

3 个答案:

答案 0 :(得分: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");

答案 1 :(得分:1)

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

ORDER BY count(*) desc

答案 2 :(得分:1)

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