WordPress popular posts by week code
-
Hello again.
I m trying make a code that list the popular posts by week.
Until now i was able to creat a veiws counter and display the 4 popular
posts.
So what i still need is to make this query more accurate.
I would like get the thumbnails and links and get the most popular
of the week.Function.php
`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’);function wpb_get_post_views($postID){
$count_key = ‘wpb_post_views_count’;
$count = get_post_meta($postID, $count_key, true);
if($count==”){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, ‘0’);
return “0 View”;
}
return $count.’ Views’;
}`
Single.php`wpb_set_post_views(get_the_ID());
<?php echo wpb_get_post_views(get_the_ID()); ?>// show 4 most popular posts
<?php
$popularpost = new WP_Query( array( ‘posts_per_page’ => 4, ‘meta_key’ => ‘wpb_post_views_count’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘DESC’ ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();the_title();
endwhile;
?>
`
- The topic ‘WordPress popular posts by week code’ is closed to new replies.