I'm looking to display a weekly list of 'Popular Posts' on a site. Currently the list shows a 'Popular Posts' column but these are posts from no particular timeframe. It's displaying the most popular posts from the beginning of the website.
Here's my code...
// Track post view count for popular posts
function track_post_view( $post_id ) {
$count_key = 'post_views_count';
$count = get_post_meta( $post_id, $count_key, true );
if( ! $count ) {
$count = 0;
}
return update_post_meta( $post_id, $count_key, ++$count );
}
// Get popular posts
function get_popular_posts( $limit = 3 ) {
// WP_Query arguments
$args = array (
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $limit,
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'post_views_count',
'meta_type' => 'NUMERIC',
);
// The Query
$popular = new WP_Query( $args );
// Restore original Post Data
wp_reset_postdata();
return $popular;
}
So, is this doable? If so, anyone able to suggest a method? I've had a look through various similar questions, but haven't found a concrete answer.
Thanks in advance.