2016-08-09 42 views
1

如何在上週顯示大多數閱讀文章?WordPress - 在上週顯示大多數閱讀文章

我有以下代碼來記錄每篇文章的點擊量,所以我會知道有多少人閱讀這篇文章。

function getPostViews($postID){ 
    $count_key = 'post_views_count'; 
    $count = get_post_meta($postID, $count_key, true); 
    if($count==''){ 
     delete_post_meta($postID, $count_key); 
     add_post_meta($postID, $count_key, '0'); 
     return "0 View"; 
    } 
    return $count.' Views'; 
} 

function setPostViews($postID) { 
    $count_key = 'post_views_count'; 
    $count = get_post_meta($postID, $count_key, true); 
    if($count==''){ 
     $count = 0; 
     delete_post_meta($postID, $count_key); 
     add_post_meta($postID, $count_key, '0'); 
    }else{ 
     $count++; 
     update_post_meta($postID, $count_key, $count); 
    } 
} 


// Remove issues with prefetching adding extra views 
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 
/** 
* Add a new column in the wp-admin posts list 
* 
* @param $defaults 
* 
* @return mixed 
*/ 

function subh_posts_column_views($defaults) { 
    $defaults['post_views'] = __('Views'); 
    return $defaults; 
} 

/** 
* Display the number of views for each posts 
* 
* @param $column_name 
* @param $id 
* 
* @return void simply echo out the number of views 
*/ 

function subh_posts_custom_column_views($column_name, $id) { 
    if ($column_name === 'post_views') { 
     echo getPostViews(get_the_ID()); 
    } 
} 

add_filter('manage_posts_columns', 'subh_posts_column_views'); 
add_action('manage_posts_custom_column', 'subh_posts_custom_column_views', 5, 2); 

如何設置以下內容以顯示上週讀取最多的帖子?

$options = array(
     'post_type' => 'post', 
     'post_status' => 'publish', 
     'posts_per_page' => $limit, 
     'ignore_sticky_posts' => true, 
     /* 'orderby' => 'meta_value_num', */ 
     'orderby' => 'rand', 
     'order' => 'desc', 
     'meta_key' => 'post_views_count' 
    ); 

回答

1

你可以在wp_postmeta表中添加'last_view_date'元數據爲每個崗位,改變了一下你的2種第一功能,並創造條件函數,對帖子返回true已經閱讀最後一週(過去7天)。

這裏是我已經定製了一下你的2個第一功能:

function getPostViews($postID){ 
    $count_key = 'post_views_count'; 
    $date_key = 'last_view_date'; 
    $today_date = date("Y-m-d"); 
    $count = get_post_meta($postID, $count_key, true); 
    if($count==''){ 
     // Note: update_post_meta() create the data if not exist 
     update_post_meta($postID, $count_key, '0'); 

     // Setting the "view date" for the first time 
     update_post_meta($postID, $date_key, $today_date); 

     return '0 View'; 
    } 
    return $count.' Views'; 
} 

function setPostViews($postID) { 
    $count_key = 'post_views_count'; 
    $date_key = 'last_view_date'; 
    $today_date = date("Y-m-d"); 
    $count = get_post_meta($postID, $count_key, true); 
    if($count==''){ 
     $count = 0; 
     // Note: update_post_meta() create the data if not exist 
     update_post_meta($postID, $count_key, '0'); 

     // Setting the "view date" for the first time 
     update_post_meta($postID, $date_key, $today_date); 

    }else{ 
     $count++; 
     update_post_meta($postID, $count_key, $count); 

     // Updating the "view date" 
     update_post_meta($postID, $date_key, $today_date); 
    } 
} 

這是有條件的功能代碼,上週看帖篩選:

// Conditional function that return true, whem a post have been read at least once in last week. 
function is_week_viewed($postID) { 
    $now = time(); 
    $date_key = 'last_view_date'; 
    $view_date = get_post_meta($postID, $date_key, true); 
    $viewdate = strtotime($view_date); 
    $datediff = $now - $viewdate; 
    $days = floor($datediff/(60*60*24)); 
    $count = get_post_meta($postID, $count_key, true); 
    if ($count > '0' && $days < 8) { 
     return true; 
    } 
} 

隨着材質,你可以設法實現在上週顯示大多數閱讀帖子爲您的搜索查詢...

然後用is_week_viewed()有條件的功能,你現在可以過濾你的搜索結果,只獲得上週的帖子。

參考 - 有條件的功能是基於這個舊線:
Finding the number of days between two dates

+0

離我非常感謝!它很棒!在我將其標記爲答案之前,我想在現場查看一些內容。 –