If you don’t want to burden your server with too many plugins, perhaps using a certain php code to display several features that usually easy to active with plugins will help your server, such as displaying related post without plugin and you can display most popular post without plugin too. This php code serves similar intention as popularity plugin, but the different is it’s php code of which we must do a little work with our function.php and other folders depend on where you want to put it.
The first thing you need to do now is back up your function.php file in case something wrong happen, and open your function.php file add this code within it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function most_popular_posts($no_posts = 10, $before = '<li>', $after = '</li>', $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 . '<a href="' . $permalink . '" title="' . $post_title.'">' . $post_title . '</a> (' . $comment_count.')' . $after;
}
} else {
$output .= $before . "None found" . $after;
}
echo $output;
} |
What we have just done is create a new function called most_popular_posts. Now all we have to do is use our new function. Open sidebar.php and add wherever you want the list to appear. Using the default Kubrick theme as an example, you would have this …
We have created a new order inside our function.php file called most_popular_posts. The next step is telling our theme to display the newly order, open your sidebar or footer or anywhere you want to add the following code:
1 2 3 | <li><h2>Popular</h2> <ul><?php most_popular_posts(); ?></ul> </li> |
If you want to control number of articles inside your popular post, you can change in the line $no_posts = 10. Thanks to RiteTurnOnly for making this tutorial available online!
Related posts:





Hi Jack!
Very useful function…
Is it possible to pass the date of the posts as well as the permalink, etc.
Thank you for sharing!
This is a pretty nice way. Plugins are always a hog on WordPress, and it is better to get done as much as possible by PHP code.
The code that you have given uses comment count to find popular posts. Is there a php script to find popular posts by page views as well?
Hello,
Good information. But when you have popular posts function set up how do you create posts and then assign them to the popular posts category? Maybe one post I want to be listed under popular posts and then some others I do not. Where is the assignment of a post to popular controlled?
Kind Regards,
Ken
I have posted a much easier way to do this if you want to check it out:
http://www.wpexplorer.com/popular-posts-without-a-plugin.html
Hello Jack !
Nice function but it is possible that i can implement this type of code to view most popular video mean which should be show only video related post according to their popularity .
Nice post!
I’ve limited space on my hosting, I’ve only 500Mb of space….
Thanks a lot!