Show Last Week’s Popular Post without using Plugins

Show last week’s uploaded popular post without using any plugins. Simple paste given code functions.php then paste short code for showing popular post.

How to Do

  • Open functions.php and paste given code there.
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');
  • Then add [show_last_week_populer] short code where you like to show popular post.
  • If you want to make design so paste style code on CSS section.
.populer_post_in_last_week a:link {
float: left;
background: #f7f7f7;
margin-top: 5px;
margin-bottom: 5px;
padding: 10px;
font-weight: bold;
}
  • Then SAVE and see popular post without using plugins. Thanks for visiting.