[英]Show trending post of a category
我正在嘗試顯示某個類別的熱門職位。 我只想顯示1個帖子。 我將趨勢定義為當前趨勢。
例如:
帖子A有100天的歷史,總共有10萬次觀看,每天約有1000次觀看。
帖子B成立10天了,瀏覽量為1萬,其中最后一天有8000。
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');
查詢帖子中今天觀看次數最多的帖子,請嘗試此操作。
$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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.