Create Your Own Popular Posts Page
100Highlighting the most popular posts on your site is a great way to send new visitors to your very best content and win them over.
There are a few different ways you can do this, and quite a few plugins out there to help. But why not base it on the number of comments? If you write a post that makes people talk, it must be a well written post (Most of the time…).
What We Will Do
What we’re going to do is quite simple, we will set up a query to get an ordered list of your most commented posts from your database, and then display those any way we like.
Popular Posts in The Sidebar
Setting this up as a sidebar widget is very easy. Just copy and paste the code below into your sidebar.php file.
To change the number of posts shown, just change the “5” at the end of line 3 to whatever number you would like.
Popular Posts
- 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) { ?>
- {}
Set Up A Separate Popular Posts Page
Instead of simply having the list in your sidebar, you could create a longer list to show a lot more of your best posts, and put this list in its own page.
Method 1 – If You’re Using the Default Theme
If you are using the WordPress default theme, then the work is mostly done for you! Just download this file, popular-posts.txt (Right Click > Save Link/Target As), then rename to popular-posts.php and upload it to your theme folder. Then skip on down this article to “Step 2 – Setting Up The Page.”
Method 2 – If You’re Using a Custom Theme
This is a little more complicated, but should still be quite straight forward.
1 – First off, open up your theme’s page.php file in a text editor (The WordPress editor won’t work here). If it doesn’t have one, then use the index.php file instead.
2 – Save this file into your theme folder (Same folder the page.php file came from!) with the new name, popular-posts.php.
3 – Paste the following code at the top of the popular-posts.php file:
1 2 3 4 5 | <!--?php /* Template Name: Popular Posts */ ?--> |
4 – Look inside the file for a section of code that will look similar to the following. The first and last lines are the important ones. The code in between doesn’t matter too much (The code you see here is from my own page.php file. Yours will not be exactly the same!).
1 2 | <!--?php if (have_posts()) : ?--> <!--?php while (have_posts()) : the_post(); ?--> |
1 2 | <!--?php the_content(' Read the rest of this entry &raquo; |
1 2 3 4 5 | '); ?>
<!-- /post -->
<!--?php endwhile; else: ?--> |
Sorry, no posts matched your criteria.
1 | <!--?php endif; ?--> |
Now, delete all of this code.
5 – In the place that you just deleted your code, paste the following instead: (Feel free to change the 5 in line 3 to any number you choose)
Popular Posts
1 2 3 4 5 6 7 | <!--?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) { ?> |
1 | <!--?php } } ?--> |
Now, in all likelihood, it will take some fiddling to get the layout right. Because your theme is custom made, there are going to be a few bits and pieces missing. Look at your page.php file and compare it to this code and try to copy over whatever is needed.
Step 2 – Setting Up The Page
Now go to your WordPress dashboard, and click Pages > Add New. Create a new page (The content in it doesn’t matter, it won’t be used) and then in the “Attributes” window, set the “Template” to “Popular Posts“. (The exact wording for those links is for WP2.7, but the Template option is still there in older versions).
Now, publish the page and go view it. You should see your most commented posts all listed with their comment counts beside them.
Using Custom Images
The code has been set up to include an image if you want to. When you publish your post, just add your image’s URL to a custom field called “popular_image” (Or change the popular_image name in the code to the name of a custom field you already use).
If you haven’t used custom fields before, you can read about them here. For all we are doing, it just means that on the post’s “Write” page, you go to the custom field’s section and type “popular_image” into the Key box, and the URL of the image into the Value box.
(I haven’t written much about custom fields here before. If you’d like to know a little more, just let me know in the comments and I’ll write up a post!)
Where to Next?
You have the code to get the posts. What you do with your page now is up to you. You can use CSS to style it any way you like. Have a look at the image below to see what milo has done with hers!
Have you tried this? Let us know if you run into any trouble with getting this working on your theme and we’ll see what we can do to help out!
WordPress Hacks Series: The code in these posts was written entirely by Milo, a web designer from Oxford, Britain. Check out her website for free WordPress themes and her blog, or follow her on Twitter!
Enjoy this post? You should follow me on Twitter!
Oh thank goodness. Being a PHP developer new to WordPress, I feared all my PHP abilities had been stripped. Now I know how to create custom pages! Thanks a ton!
great blog post your website is very well designed like you have a good fashion sense.
I have always created my own! This is Very helpful!!! Thanks for the Info!
I’ve always just created my own manually. This should help me in my efforts to make things a little better when I move to the wordpress platform.
Thanks
This was something that I was considering for my site further down the line. One thing I will open up for debate though is grabbing the posts by the number of comments.
While obviously this is a pretty good indication of how popular a post is, is it really the best way? For example there are pages that may get more traffic which isn’t indicated by the number of comments. Or there are popular pages that perhaps don’t really warrant being there. (Maybe you posted it was your birthday for example.)
Wouldn’t a better way be to hand pick the posts yourself, or perhaps run a poll on your site. You could then theme the pages to show posts that are more related to each other and perhaps order them somewhat if required.
To do this I would just make a new page (I have created a template on my site without sidebars as well so I can pick and choose) and then add the specific links in manually to that page in the wordpress admin.
Another thing that pops to mind is should you do a later follow up post on a previous one with updated info. Now if the previous one was by chance your most commented post, that would still show up instead of the new one. Obviously with a follow up post it is good habit to go back and edit the previous one with a link to the new one saying there has been an update on it, should people find the way to your site directly to the old post.
Is there a simple way to not include a specific post by its id?
I’m interested to see what others think on this.
(oh and the authors website messes up in google chrome here)
Thanks for the article. Very useful.
wow thanks this will help me customize my blog to look more professional.
corsests uk
Useful article, thanks for sharing
Very easy to follow post, thank you very much!
I have to agree with Jon, “popular posts” may just be posts that have not a lot to do with what you are actually trying to promote / show / sell on the site, as such it might be worth being able to manually add links.
But other than that, if you have a very precise field of stuff you are posting about then this is the code to do it with :)
Another option for folks like me who prefer not to play around with code is http://www.postrank.com. It’s an updated version of the aiderss product. They have a very nice looking widget that constantly updated to display your most popular content (per their algorithm, of course).
Obviously, you don’t have the flexibility you do with this solution, but I’ve played around with it a bit and it seems pretty nice. So, just passing along in case it’s a fit for anyone out there.
awesome write up there with the right steps to help a noob out….much appreciated!
I started with WordPress not so long ago… Still sceptic about using it as a CMS, but thanks to this post I know how to create custom pages (templates) and that’s a great step forward!! Great article!
I’ll have to explore this, Michael. On my blog, I have the plugin WP-Stats installed. Every once in a while I look at the stats for Most Viewed Posts and Most Commented Posts and tweak the list I’ve created in a text widget in my sidebar. That seems to work for me. I don’t include a post that is still the list of most recent posts. Once it has been long enough for a recent popular post to be off the sidebar, I’ll add it to the list I maintain and rotate another one off.
Thanks for the tutorial. That is indeed very useful to have. I am sure there are plugins for this, but as this is custom coded, it gives a lot space for customization.
OK, I wasn’t going to do this so soon, but I decided that it was a good way to declutter my sidebar a bit by putting the readers’ favorite posts on a separate page. Two downsides, though, is that this method doesn’t take into account the number of page views in addition to the number of comments and that it requires an extra click to get to that page rather than its being in the sidebar.
We’ll see how it goes. Thanks for great instructions that work, Michael!
Wow. Thanks for sharing and I should try it
I’ve created my own custom pages in WordPress before, but not one which serves this function. A good tut to fall back on when I need it – thanks.
Thank you so much for this post! I have been trying to figure this out for a while now…
Amazing tutorial! I’m kind of new to the whole blogging thing and I try to learn new things about it everyday. At my pace, with your post you’ve filled my 2 week learning quota ;)
Thanks again, i will be coming back for more :)
It looks promising, i will give a try!
Thanks!
Good tutorial here, Nice step by step instruction. For a guy like me, learning to code around WordPress, all the help I can get is good.
Cool tips here, thank for sharing!
Hmm, I just don’t understand at all about PHP script because I used Blogger. However I like this post.
That’s a very nice tutorial. “Popular posts” always make an average visit time longer and can also get you couple new subscribers. Cheers.
Nice, but this method is based on number of comments. What about popular posts based on visits? Is it possible?
Thanks for the tutorial, there is never ending things to learn. Have a great day!
I also search method for how to make wordpress blog for my website your article help me to make my website blog thanks for this sharing method.
Hi. Nice tutorial, but how do i make a top viewed post or top rated posts ? also i’m trying to put the results in 2 columns but not a chance …
I hope you can help me to make a top viewed/rated posts page.
Thx.
this is a very nice post.
very useful. i’ve been using a plugin but want to integrate into code as much as i can.
thanks again
How can you limit the date range for most popular/commented posts? Also, can you have several instances of this running on the same page (i.e. Most Popular for 2008, 2009, etc…)? Thank you.
pretty useful.. thanks.!
.-= tasarhane´s last blog ..tasarhane wrote a new blog post: 2009 Açıköğretim Final Sınav Sonuçları =-.
Useful article, thanks for this hack
.-= Erjon broshka´s last blog ..Komplet lista e figurinave qe chati facebook disponon.. =-.
Hi, is it possible to show published date and category with this hack? I tried to modify it by myself but the result is a mess…
Thanks a lot for your help!
This is a really useful post. I especially like the fact that you have made it so easy to follow.
Thanks,
Karl
For those that can’t code well there is a plugin that will do the above(based on comment count) and works with wordpress 2.8.4 called Most Comments and be found at http://wordpress.org/extend/plugins/most-comments/, you can easily install it on your sidebar or anywhere else for that manner.
nice information ,and it is work ^^ ,thanks :D
It is concerned with the size of organisms, more particularly with the size of our own corporeal selves. ,
I got more useful information according to best posts in this page on side column
yes it will be better if there will be more
Simply perfect. I was having some issues with plugins and it works fast and clean.
Thanx!
Thank you very much for the tutorial
But I have the same doubt as Shane. Is it possible to sort the popular posts within a given period, for example in the range of five days or last week? (by editing php)
i’d appreciate a lot your help
Thanks a lot for this article! I still consider WordPress a blog only system and I got a lot to learn about WordPress “hacking”. Things like these help me a lot.
Hi,
How to create “Popular posts this month” in Wp content area? Example in tuts+ frontpage. Please, help me.
How would I ammend this code to display most popular posts by pageviews?
Did you ever find out how to amend the code to display most popular posts by pageviews?
You must be using some stats plugin to determine post views first. Then there is an adjustment necessary here $commentcount = $post->comment_count; (I hope so!)
Just google it, you’ll find a bunch of answers. :)
WordPress tehems for Tamil 5. Fixing WordPress tehems to display Tamil properly in Firefox 6. Fixing jilebi font or ???? issue for self-hosted blogs in Tamil for WordPress 2.0+ 7. WordPress plugin for Thamizmanam toolbar Tags: tamil in wordpress,
Thanks for the tutorial. I just finished setting up my popular posts page using your guide, and it looks great!
Good job with the tutorials, keep up the good work!
I found this extremely helpful! Thank you!
Definetelly it wiill be benificial for blog popularity.
So thanks for this post.
Your post will be rather good, and I’m sure some will find it interesting because it’s about a topic that’s as widely discussed as others. Some may even find it useful.
Thanks so much for your post.
Thanks for displaying an image of the html, it helps beginners out a lot.
Thanks for the tutorial. That is indeed very useful to have. I am sure there are plugins for this, but as this is custom coded, it gives a lot space for customization.
I appreciate the work that your are doing!!
It gives me a whole bunch of ideas. this is great. thanks anyway.
Check it out my page if you enjoy writing, reading poems/short stories/haiku here is the place….
A community page where everyone are welcome to post their poems or anything relating to that. In UAE we find a lot of poems writers in Arabic but this will be a page for all English writers including all ages & nationalities…
A community page where everyone are welcome to post their poems or anything relating to that. In UAE we find a lot of poems writers in Arabic but this will be a page for all English writers including all ages & nationalities…
http://www.facebook.com/pages/Share-your-poems/113862481998401
When you plan to create your post page, it is important that you make it interesting and attractive to get the interest of the viewers.
i use blogger :((
maybe I’ll try wordpress, soon :D
do this, and quite a few plugins out there to help. But why not base it on the numbe
awesome write up there with the right steps to help a noob out….much appreciated!
This post has given me an idea, thanks
This is based on number of comments, but if I want to show a list of posts with highest number of post views, then how should it be?
Good to see other perspectives.
thank you for sharing
it is important that you make it interesting and attractive to get the interest of the viewers. (:
thanks :D
i like :(
Great! This information will definitely be used. I have been looking for this information, but most sites confuse things more than they have to be. Thanks and keep up the good work.
Obviously with a follow up post it is good habit to go back and edit the previous one with a link to the new one saying there has been an update on it, should people find the way to your site directly to the old post.
Good to see other perspectives.
The post is written in very a good manner and it entails many useful information for me.
This information will definitely be used. I have been looking for this information, but most sites tenant Screening confuse things more than they have to be. Thanks and keep up the good work.
This information will definitely be used. Thanks for the tutorial. That is indeed very useful to have. I am sure there are plugins tenant Screening for this, but as this is custom coded, it gives a lot space for customization.
This information will definitely be used. I have been tenant Screening looking for this information, but most sites confuse things more than they have to be.
This article is truly relevant to my study at this moment, and I am really happy I discovered your website.
Buy Wow Gold from us today. “Any man who has to ask about the cost of an epic mount can’t afford one.” – The only solution… Buy some!
Once this requires place and they arrive to be “IT” handbags then everyone has to have it. The only element incorrect with this is the truth that these Hollywood Darlings can very easily afford to get the bag they’re carrying for free!
It gives me a whole bunch of ideas. this is great. thanks anyway.
Soccer is my favorite,so is David Beckham
this is very good.I like it .
I was given a Chi Turbo as a gift, and I just found this version to be way more durable than that original one. The base of flat iron itself is thicker, providing the ceramic plates more protection if it was to be dropped on the bedroom floor.
i’ve done it.. it turns out very nice…
but, care to share the css code?
i have difficulty in css
:D
want to Earn 100 $ Per Hour
http://skinet.adfoo.info/?r=4.
This is one good blog to keep up.
file download not working
it’s done, i want to add excerpt under post title how to ?
sehr gut und interresant.
gruss ralph
Some times its a pain inside the ass to read what blog owners wrote but this web site is truly user pleasant! .
Thanks a lot for sharing this. I have never thought that surfing online can be so much beneficial and entertained in a good shape. I feel really happy and grateful for providing me with such priceless sound track. All are good here simply best. I want such article again and again
ss
Coucou vous quel est votre point de vue de mon nouveau site sur le diagnostic immobilier?
There is one problem with the code, it also shows the deleted posts too… please fix this
Virtually all of the responses on this particular website dont make sense.
Could you please explain how to put recent post and popular post within a single widget.. Initially the recent posts are visible..On clicking the popular post menu, list of popular posts got displayed. I found it on http://unwiredview.com/. (Not my website)
Is there any widget available or I have to write the code manually ? That looked very elegant and cute. I am not an expert in coding.Could you please help me ?
really wonderful information which helps alot ..
I was wondering if you ever considered changing the page layout of
your blog? Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content
so people could connect with it better. Youve got an awful lot of text for
only having 1 or two images. Maybe you could space it out
better?
Thanks for the code .. popular post code is very useful for me
Great! This information will definitely be used. I have been looking for this information, but most sites confuse things more than they have to be. Thanks and keep up the good work.
very good!!!!!!!!!!!!!!!
Thanks for the sharing of the method, but it look like quite difficult. Anyway I will try it.
hi michael.., your article seems very intresting and very logical., so keeping in mind your programming capablities i have a query for you. how do we create sub widget in the wordpress blogs so, that i could put few articles in it..
for examples you can refer to the computer language totrial websites, there you can see that while we choose our preffered programming language. than a new widget comes into its page, which is static for all content/tutorials of that programming language..,
plz tell me how to do that.., i am planning to build such a site., need your help.
for any query related to question comment below.