2017-06-26 23 views
0

カテゴリのトレンドポストを表示しようとしています。私は1つの投稿を表示したいだけです。私はトレンドを今のトレンドと定義します。例えばカテゴリのトレンドポストを表示

:1K日ビューの周り

ポストAは100日齢で、合計10万のビューを持っていました。

ポストBは10日齢で、10Kビュー、最終日に8Kビューでした。

Aは長らく普及していますが、Bはこの時点で傾向があります(それ以前はAよりも日常的にトラフィックが少なくて済みました)。

<?php 

$today = getdate(); 
$args = array(
      'meta_key'  => 'post_views', 
      'meta_value' => '1000', 
      'meta_compare' => '>=', 
      'orderby' => 'meta_value_num', 
      'ignore_sticky_posts' => 1, 
     'cat' => 14, 
     'numberposts' => 1, 
      'paged' => $paged, 
      'date_query' => array(
       array(
         'year' => $today['year'], 
         'month' => $today['mon'], 
         'day' => $today['mday'], 
    ), 
), 
); 
$trenquery = new WP_Query($args); 

// The Loop 
if ($trenquery->have_posts()) { 

get_the_title(); 

    /* Restore original Post Data */ 
    wp_reset_postdata(); 
} else { 
    // no posts found 
} 

のfunctions.php

// Popular Posts 
 
function wpb_set_post_views($postID) { 
 
$count_key = 'wpb_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); 
 
} 
 
} 
 
//To keep the count accurate, lets get rid of prefetching 
 
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 
 

 
function wpb_track_post_views ($post_id) { 
 
if (!is_single()) return; 
 
if (empty ($post_id)) { 
 
global $post; 
 
$post_id = $post->ID; 
 
} 
 
wpb_set_post_views($post_id); 
 
} 
 
add_action('wp_head', 'wpb_track_post_views');

答えて

0

クエリ今日のために最も見られた記事の投稿、これを試してみてください。

$today = getdate(); 
$args = array(
    'meta_key'   => 'post_views_count', 
    'orderby'   => 'meta_value_num', 
    'posts_per_page' => 1, 
    'post_type'   => 'post', 
    'post_status'  => 'publish', 
    'date_query'  => array(
     array(
      'year' => $today['year'], 
      'month' => $today['mon'], 
      'day' => $today['mday'] 
     ) 
    ) 
); 
$my_query = new WP_Query($args); 

if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); 

     get_post_meta(get_the_ID(), 'post_views_count', true); 

    endwhile; 
    wp_reset_postdata(); 
endif; 
+0

これは機能しませんでした。投稿は表示されません。 – user6738171

+0

あなたのポストはポストビューのカウント機能を持っています。データベース全体を検索するには "post_views_count"というキーワードをチェックしてください。もしこのメタがあなたのwp_postmetaテーブルを持っていれば、それだけが動作します。 – GNANA

+0

私はこの機能を自分の質問に追加しました。この機能を使用しても、それはまだ機能しません。 – user6738171

関連する問題