2016-08-09 5 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

は、あなたは2最初の機能ビットを変更し、最後に読まれた記事のためにtrueを返す条件関数を作成し、wp_postmetaテーブル内の各ポストのため'last_view_date'のメタデータを追加することができます週(過去7日間)。ここで

は、私は少しカスタマイズしたあなたの2つの最初の関数です:あなたは、その材料で

// 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; 
    } 
} 

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); 
    } 
} 

は先週フィルタリングするために、これは条件関数のコードで投稿を見ましたあなたの検索クエリのための先週のほとんどの読んだ記事を表示することを達成できます...

次にis_week_viewed()条件付きの機能検索結果をフィルタリングして、先週の投稿のみを取得することができます。

リファレンス - 条件付きの関数は、この古いスレッドに基づいています。私から
Finding the number of days between two dates

+0

ビッグありがとう!それは素晴らしい作品です!私がそれをマークする前に、私はライブサイトで何かをチェックしたい。 –

関連する問題