admin管理员组文章数量:1130349
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
1 Answer
Reset to default 0This 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.
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
1 Answer
Reset to default 0This 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.
本文标签: wp queryShowing most popular post of week
版权声明:本文标题:wp query - Showing most popular post of week 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749215428a2334077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论