1
我目前正在帮助我的朋友优化她的网站。它建立在wordpress主题上,并且有一个ViewsPost插件用于只显示观看次数最多的帖子。我需要做的是重写SQL,以便在上周得到最多查看的帖子。在WHERE条件下,只能说获得那些日期为<的帖子比当前日期。如何编辑它?Wordpress PostViews插件按日期排序
<?php
}
function gen21_display_most_viewed_posts($smallthumbs = true) {
global $wpdb;
/* taken from wp-postviews.php by Lester Chan http://lesterchan.net/portfolio/programming/php/ */
$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 < '".current_time('mysql')."' AND post_type = 'post' AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT 4");
if (!$favourites) return false;
foreach ($favourites as $fav) { ?>
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12 item">
<a class="front-item" href="<?php echo get_permalink($fav); ?>">
<?php
echo get_the_post_thumbnail($fav, 'thumbnail');
/*
if ($smallthumbs) {
echo get_the_post_thumbnail($fav, 'small');
} elseif (has_post_thumbnail($fav)) {
if (rand(0,1) == 1) {
echo get_the_post_thumbnail($fav, "medium");
}
else {
echo get_the_post_thumbnail($fav, "small");
}
} */ ?>
我改变了这一点,但它仍然显示2014年的帖子@ishegg –
也许有一个视图缓存?尝试直接在数据库中运行查询(即在phpMyAdmin中)。它肯定不应该从2014年获得结果。 – ishegg
我在你的解决方案中发现了一个错误:'post_date BETWEEN(DATE_SUB(NOW(),INTERVAL 1 WEEK))和DATE(NOW())'。我只是添加了日期字和一个手镯。无论如何,随时编辑你的答案,我会接受它。谢谢! @ishegg –