php Code for showing last week’s popular Items

Use this code on functions.php, then paste [show_last_week_populer] shortcode for showing last week’s popular Items.

function popular_posts_last_week() {
global $wpdb;
$most_visited_posts = $wpdb->get_results("SELECT p.ID, p.post_title, pm.meta_value AS 'views' FROM {$wpdb->prefix}posts AS p JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id WHERE p.post_status = 'publish' AND p.post_type = 'post' AND pm.meta_key = 'post_views_count' AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= p.post_date ORDER BY views DESC LIMIT 5");
echo '<div class="populer_post_in_last_week">';
foreach ($most_visited_posts as $post) {
echo '<a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a>';
}
echo '</div>';
}

add_shortcode('show_last_week_populer', 'popular_posts_last_week');