gpt4 book ai didi

php - 按日期排序的 Wordpress PostViews 插件

转载 作者:行者123 更新时间:2023-11-30 21:54:43 27 4
gpt4 key购买 nike

我目前正在帮助我的 friend 优化她的网站。它建立在 wordpress 主题之上,并且有一个插件 ViewsPost 用于仅显示查看次数最多的帖子。我需要做的是重写 SQL,使其仅获取上周查看次数最多的帖子。在 WHERE 条件下,只能说得到那些日期小于当前日期的帖子。如何编辑?

<?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");
}
} */ ?>

最佳答案

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

关于php - 按日期排序的 Wordpress PostViews 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45723510/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com