因此,如果您想获取在过去xx小时内(例如12小时)发表评论的帖子,请尝试以下操作之一:
请注意,我只选择已发布的帖子(post_status = \'publish\'
) 以及post
类型(post_type = \'post\'
).
只需替换$hours
使用您喜欢的小时数
使用TIMESTAMPDIFF()
.
$hours = 12;
$result = $wpdb->get_results( $wpdb->prepare( "
SELECT p.comment_count, p.ID, p.post_title
FROM $wpdb->posts p
INNER JOIN $wpdb->comments c ON c.comment_post_ID = p.ID
WHERE p.post_type = \'post\'
AND p.post_status = \'publish\'
AND ( TIMESTAMPDIFF( HOUR, c.comment_date, NOW() ) <= %d )
GROUP BY p.ID
ORDER BY p.comment_count DESC
LIMIT 0, 25
", $hours ) );
或计算xx小时前的日期和时间,并使用
comment_date >= <date-time xx hours ago>
.
$hours = 12;
$timestamp = current_time( \'timestamp\' );
$date_ago = date( \'Y-m-d H:i:s\', $timestamp - $hours * HOUR_IN_SECONDS );
$result = $wpdb->get_results( $wpdb->prepare( "
SELECT p.comment_count, p.ID, p.post_title
FROM $wpdb->posts p
INNER JOIN $wpdb->comments c ON c.comment_post_ID = p.ID
WHERE p.post_type = \'post\'
AND p.post_status = \'publish\'
AND c.comment_date >= %s
GROUP BY p.ID
ORDER BY p.comment_count DESC
LIMIT 0, 25
", $date_ago ) );