admin管理员组文章数量:1130349
The plugin Most Popular Posts doesn't support WPML and therefore I have tried creating my own.
I have found this tutorial in creating your own code for showing the most popular posts on my site: How to Display Popular Posts by Views in WordPress without a Plugin
However this doesn't take in the factor of per week. I would like it to be pointed in the right direction on how to do this.
This code updates the posts actual view-count:
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);
Add these fields to the post:
week_count: integer
current_week: datetime
Check if current_week matches the actual current week, otherwise reset the week_count and add 1 and set the current_week to the actual current week.
Is there another way of doing this in a smarter and more effiecient way?
The plugin Most Popular Posts doesn't support WPML and therefore I have tried creating my own.
I have found this tutorial in creating your own code for showing the most popular posts on my site: How to Display Popular Posts by Views in WordPress without a Plugin
However this doesn't take in the factor of per week. I would like it to be pointed in the right direction on how to do this.
This code updates the posts actual view-count:
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);
Add these fields to the post:
week_count: integer
current_week: datetime
Check if current_week matches the actual current week, otherwise reset the week_count and add 1 and set the current_week to the actual current week.
Is there another way of doing this in a smarter and more effiecient way?
Share Improve this question edited Sep 1, 2016 at 13:08 Ethan Rævan 4,0295 gold badges27 silver badges55 bronze badges asked Nov 21, 2014 at 10:34 PhilipPhilip 2074 silver badges15 bronze badges 5 |1 Answer
Reset to default 2Okay, so here is the complete query for displaying popular posts of current week. I am using meta_query to limit query results within current week only.
It will get all posts from current week and then sort them by post views count added by custom field wpb_post_views_count that you used in your question.
// Current week's popular posts
$query_args = array(
'post_type' => 'post',
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
'meta_key' => 'wpb_post_views_count',
'orderby' => 'meta_value_num',
'ignore_sticky_posts' => 1,
'posts_per_page' => '-1',
);
$my_query = new WP_Query( $query_args );
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
// add your loop content here.
endwhile;
endif;
wp_reset_postdata();
The plugin Most Popular Posts doesn't support WPML and therefore I have tried creating my own.
I have found this tutorial in creating your own code for showing the most popular posts on my site: How to Display Popular Posts by Views in WordPress without a Plugin
However this doesn't take in the factor of per week. I would like it to be pointed in the right direction on how to do this.
This code updates the posts actual view-count:
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);
Add these fields to the post:
week_count: integer
current_week: datetime
Check if current_week matches the actual current week, otherwise reset the week_count and add 1 and set the current_week to the actual current week.
Is there another way of doing this in a smarter and more effiecient way?
The plugin Most Popular Posts doesn't support WPML and therefore I have tried creating my own.
I have found this tutorial in creating your own code for showing the most popular posts on my site: How to Display Popular Posts by Views in WordPress without a Plugin
However this doesn't take in the factor of per week. I would like it to be pointed in the right direction on how to do this.
This code updates the posts actual view-count:
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);
Add these fields to the post:
week_count: integer
current_week: datetime
Check if current_week matches the actual current week, otherwise reset the week_count and add 1 and set the current_week to the actual current week.
Is there another way of doing this in a smarter and more effiecient way?
Share Improve this question edited Sep 1, 2016 at 13:08 Ethan Rævan 4,0295 gold badges27 silver badges55 bronze badges asked Nov 21, 2014 at 10:34 PhilipPhilip 2074 silver badges15 bronze badges 5-
You can use
WP_Queryto get posts from database but how do you want to limit time period. 1. You can get last 7 days posts or 2. posts from current week. ?? – Robert hue Commented Nov 21, 2014 at 10:39 - Are your sure that "Most popular posts" does not support WPML????? i.imgur/rUG6pRa.jpg – cybmeta Commented Nov 21, 2014 at 10:45
- @cybmeta - Yeah, Im sure, -> wordpress/support/topic/… – Philip Commented Nov 21, 2014 at 12:08
- @Roberthue - No not posts from current week, I want to have the most viewed post from current week. – Philip Commented Nov 21, 2014 at 12:11
- @Philip Has this quesiton been resolved? – Ethan Rævan Commented Sep 1, 2016 at 12:20
1 Answer
Reset to default 2Okay, so here is the complete query for displaying popular posts of current week. I am using meta_query to limit query results within current week only.
It will get all posts from current week and then sort them by post views count added by custom field wpb_post_views_count that you used in your question.
// Current week's popular posts
$query_args = array(
'post_type' => 'post',
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
'meta_key' => 'wpb_post_views_count',
'orderby' => 'meta_value_num',
'ignore_sticky_posts' => 1,
'posts_per_page' => '-1',
);
$my_query = new WP_Query( $query_args );
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
// add your loop content here.
endwhile;
endif;
wp_reset_postdata();
本文标签: phpShow the most popular post per week
版权声明:本文标题:php - Show the most popular post per week 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749214689a2333964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


WP_Queryto get posts from database but how do you want to limit time period. 1. You can get last 7 days posts or 2. posts from current week. ?? – Robert hue Commented Nov 21, 2014 at 10:39