2017-08-16 79 views
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"); 
        } 
       } */ ?> 

回答

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"); 
+0

我改變了這一點,但它仍然顯示2014年的帖子@ishegg –

+0

也許有一個視圖緩存?嘗試直接在數據庫中運行查詢(即在phpMyAdmin中)。它肯定不應該從2014年獲得結果。 – ishegg

+0

我在你的解決方案中發現了一個錯誤:'post_date BETWEEN(DATE_SUB(NOW(),INTERVAL 1 WEEK))和DATE(NOW())'。我只是添加了日期字和一個手鐲。無論如何,隨時編輯你的答案,我會接受它。謝謝! @ishegg –