Most viewed posts with out using a plugin

Paste the following code where you want to display most viewed posts


$querydetails = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = ‘post_views_count’
AND wposts.post_status = ‘publish’
AND wposts.post_type = ‘post’
ORDER BY CAST(wpostmeta.meta_value AS DECIMAL) DESC LIMIT 5

$pageposts = $wpdb->get_results($querydetails, OBJECT)


  • Most Popular

      foreach ($pageposts as $post):
      setup_postdata($post); ?>

  • Leave a Reply