I had the same issue. I had searched many times for this issue. but I could not find a solution for this.
get the most viewed post for the last 2 days not to get the posts for the last 2 days
I have a table named “wp_popularpostssummary”.
Result ll be
so from this table get result postids by using following custom query
$results = $wpdb->get_results($wpdb->prepare('SELECT postid,COUNT(*) AS qy FROM `wp_popularpostssummary` WHERE `view_date` BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 1 WEEK) AND CURRENT_DATE() GROUP BY postid ORDER BY qy DESC'));
query result ll be the post id’s of most viewed posts for the last one week.
then using following foreach loop to get post id
$pids = [];
$results = json_decode(json_encode($results), true);
foreach ($results as $result) {
foreach ($result as $k => $v) {
if($k == 'postid'){
array_push($pids, $v);
}
};
}
finally, use post id to fetch post details. code ll be following
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '10',
'orderby' => 'post__in',
'post__in' => $pids
);
$query = new WP_Query( $args ); if ( $query->have_posts() ) {
// The Loop
while ( $query->have_posts() ) { //code here
}
}
Change date duration according to you needs.
I hope this ll help to anyone in the feature!
Let me know if have a query.