$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Showing most popular post of week|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Showing most popular post of week

matteradmin10PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far