How-to show popular posts on your WordPress blog?
Yesterday I replaced my list of current PHP articles on this blog with a list of popular articles. I’m sure it’s possible in WordPress to get those list if you count the number of comments, but actually I was looking for solution based on real traffic.
Since I’m using WordPress.com Stats, I would like to use the rankings generated by this service or plugin. Searching Google, I found some widget called “WordPress.com Stats: Top Posts Widget” which works out of the box (if your like to use a widget). In my case I have a custom sidebar with different custom sections using custom code. The following example explains how-to use that code on your website.
First you need to install a WordPress.com Stats plugin
Yes, a WordPress Stats plugin is required, because the list is based on that data. It’s possible that you need to wait a couple of days before you can continue with the code below (the system need to store some data first).
Update!
In 2011 Automatic has included the WordPress Stats plugin in their new Jetpack plugin distribution tool. A views later the developer removed the “old” plugin and you need to install Jetpack or Jetpack Lite which offers only the WordPress stats plugin and the WP.me URL shortener function.
The tutorial code
Locate the place where you like to add the list: that can be inside the sidebar, the footer or maybe on your blog’s homepage. Add this code snippet:
<?php echo ' <h2>Most Popular Articles</h2> <ul>'; $max = 5; $top_posts = stats_get_csv('postviews', "days=7&limit=10"); foreach($top_posts as $post) { $post_obj = get_post($post['post_id']); if (isset($post_obj) && $post_obj->post_type == 'post' && $cnt < $max) { echo ' <li><a href="'.get_permalink( $post_obj->ID ).'">'.$post_obj->post_title.'</a> ('.$post['views'].')</li>'; $cnt++; } } echo ' </ul>'; ?>
Update 9th June 2012:
I modified the code snippet because the CSV stats from wordpress.com doesn’t provide the correct post URL if you use the category as part of your permalink structure (f.e. /%category%/%postname%/). The object named $post_obj has all the information I need and while using the function get_permalink() I get the right URL for each single post in my list. That will say we use only the ID and the number of views from the CSV-stats file.
This snippet is very easy to explain, first let us look on the first two rows:
$max = 5;
$top_posts = stats_get_csv('postviews', "days=7&limit=10");
I created a variable that holds the number of entries I would like to show. Inside the function it’s possible to add the values for the time period and a limit for the number of posts. This number is higher than the number I’ve used before. Running that function will create an array named $top_posts with this kind of key/value pairs:
[0] => Array (
[post_id] => 3933
[post_title] => Sending e-mails via SMTP with PHPmailer and Gmail
[post_permalink] => https://www.web-development-blog.com/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/
[views] => 819
)
[1] => Array (
[post_id] => 5249
[post_title] => Parse html with PHP preg_match_all()
[post_permalink] => https://www.web-development-blog.com/parse-html-with-preg_match_all/
[views] => 555
)
[2] => Array (
[post_id] => 3249
...
)
Actually this array has all the information I need for my list, it’s possible that this array contains other pages too. I need to get the post type for each post and use that value as a filter inside the “foreach” loop. The “if” statement is used as control source to show only the maximum number of posts as declared before. Our limit (10) inside the function is higher than the “real” limit, this way we have “enough” possible elements to work with.
What are the advantages?
It depends on the traffic you get. If one post is getting most of your site’s traffic, the chance is bigger that more people will share an article with others. This activity might provide more traffic to the same post and of course many other posts. There is also a great advantage for your on-site SEO, because these popular posts are listed on your homepage (and maybe site-wide), they might getting more “link juice” than posts without these links. This authority is helps all the pages linked from your popular pages list.
Published in: WordPress Development
Hi Olaf,
I so love being able to use custom code to display popular posts without having to rely upon a plugin. I have far more control how things are displayed that way.
Only problem is with my permalink structure, the URLs being returned aren’t quite rite. I have removed the category base and am using the category name as part of the URL. Your code seems to take issue with the category name – it does not include it.
Your code does find the most popular posts correctly but there seems to be something off with the permalinks.
Any idea what I am doing wrong with my permalinks or is it something that’s changed since you first wrote this post?
Thank you for building such a great WordPress resource!
Michele
Hello Michele,
thanks for the bug report. I used the snippet for my previous (widget-less) theme and never noticed that problem. I’ve update the tutorial with the right code and some extra information.
Olaf
Oh yay! It works now!
One more question…
Does the limit need to be exactly twice $max or just it just need to be a larger number?
Hi Michele,
it depends on how many “other” pages are popular too. For example if your site has 3 popular pages (homepage, contact, faq) and you like to show at least 5 posts, you need to request at least 8 results (or 10 to be safe).