Show Your Blog’s Most Popular Posts Without Using Any Plugin
To show your visitors Most Popular Posts from your blog you can use the following code. This code snippet will gather a list of top posts based on comments count on that post. More comments to a post makes it more popular.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <h2>Popular Posts</h2> <ul> <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5"); foreach ($result as $post) { setup_postdata($post); $postid = $post->ID; $title = $post->post_title; $commentcount = $post->comment_count; if ($commentcount != 0) { ?> <li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"> <?php echo $title ?></a> {<?php echo $commentcount ?>}</li> <?php } } ?> </ul> |
You can add this code to your Sidebar.php if you want to show Most Popular Post List in your blog’s sidebar. If you want to show more than 5 posts at a time simply find comment_count DESC LIMIT 0 , 5″); in above code and change ‘5‘ to 10 or 15.




