wp query - Showing most popular post of week

admin2025-06-06  1

I'm trying to display the most popular post of the past week. However, I it is not working correctly and showing the most popular (most viewed) post overall. I know my code works because I was able to view the most popular posts of the last month by using monthnum'=>$month. How can I fix this to show the most popular post of the past week?

<?php 
$week = date('W'); 
$year = date('Y'); 
$new_query = new WP_Query( array('posts_per_page' => 1, 'cat' => 14, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC','weeknummer'=>$week,'year'=>$year )); 
?> 
<?php if ( $new_query->have_posts() ) : ?> 
<?php while ( $new_query->have_posts() ) : $new_query->the_post(); ?> 
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?> 
<?php else : ?> 
<?php endif; ?> 
// 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');

I'm trying to display the most popular post of the past week. However, I it is not working correctly and showing the most popular (most viewed) post overall. I know my code works because I was able to view the most popular posts of the last month by using monthnum'=>$month. How can I fix this to show the most popular post of the past week?

<?php 
$week = date('W'); 
$year = date('Y'); 
$new_query = new WP_Query( array('posts_per_page' => 1, 'cat' => 14, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC','weeknummer'=>$week,'year'=>$year )); 
?> 
<?php if ( $new_query->have_posts() ) : ?> 
<?php while ( $new_query->have_posts() ) : $new_query->the_post(); ?> 
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?> 
<?php else : ?> 
<?php endif; ?> 
// 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');
Share Improve this question asked Jun 26, 2017 at 3:47 user7548188user7548188 291 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This is not how you collect view stats over period of time. The right way to do it is to have some sort of sliding window that will help you calculate views over specific time frame. For example to get the most popular post "today" you store the counts in an option, and zero the option every 24 hours. For the most popular in the last week, you use an option per day, remove the "oldest" one when it expires, and everyday recalculate the most popular in the last 7 days.

What your code actually does right now is to show the most popular post of those published last month, which is unlikely to be what you want.

Side note: writing to the DB on front end requests is a great way to bring a site down (you will not notice it on small site, but once you start having some good traffic... boom).... and obviously your method will not work with page caching.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749215454a317330.html

最新回复(0)