How to display popular posts without plugin?

The same as “How to display random posts without plugin”, you can also display polular posts without plugin.

Copy codes below and paste to your theme:

< ?php function popular_posts($no_posts = 5, $before = '
  • ', $after = '', $show_pass_post = false, $duration='') { global $wpdb; $request = "SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments"; $request .= " WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish'"; if(!$show_pass_post) $request .= " AND post_password =''"; if($duration !="") { $request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$duration." DAY) < post_date "; } $request .= " GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC LIMIT $no_posts"; $posts = $wpdb->get_results($request); $output = ''; if ($posts) { foreach ($posts as $post) { $post_title = stripslashes($post->post_title); $comment_count = $post->comment_count; $permalink = get_permalink($post->ID); $output .= $before . '' . $post_title . ' (' . $comment_count.')' . $after; } } else { $output .= $before . "None found" . $after; } echo $output; } ?> < ?php most_popular_posts(); ?>
  • You can change the number of posts you want to show by change the “5″ in “$no_posts = 5″. Enjoy it!

    This entry was posted in Theme Tips and tagged , . Bookmark the permalink.

    One Response to How to display popular posts without plugin?

    1. 99839 says:

      wa!.i love this

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>