按日期排序的Wordpress PostViews插件

时间:2017-08-16 21:45:40

标签: php mysql sql wordpress

我正在帮助我的朋友优化她的网站。它建立在wordpress主题上,并且有一个插件ViewsPost用于仅显示观看次数最多的帖子。我需要做的是重写SQL,以便它只获得上周查看次数最多的帖子。在WHERE条件中,只有被称为获得日期为<比当前日期。如何编辑?

dynamic_cast

1 个答案:

答案 0 :(得分:0)

更改post_date条件:

post_date BETWEEN DATE_SUB(NOW(), INTERVAL 1 WEEK) AND NOW() -- or
post_date BETWEEN (DATE_SUB(NOW(), INTERVAL 1 WEEK)) AND DATE(NOW())

这会从当前日期减去一周,并找到post_date介于获得日期和当前日期之间的帖子

整个查询是:

$favourites = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date BETWEEN DATE_SUB(NOW(),INTERVAL 1 WEEK) AND NOW() AND post_type = 'post' AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT 4"); // or
$favourites = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date BETWEEN (DATE_SUB(NOW(),INTERVAL 1 WEEK)) AND DATE (NOW()) AND post_type = 'post' AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT 4");