显示类别的趋势帖子

时间:2017-06-26 02:35:04

标签: php wordpress

我正在尝试显示某个类别的热门帖子。我只想显示1个帖子。我将趋势定义为趋势。

例如:

帖子A是100天大,总观看次数为100K,每天观看次数约为1K。

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

&#13;
&#13;
// 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');
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分: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;