Many thanks for the information on: Wordpress popular posts without using plugins which greatly helped me put together my own Popular Posts Page Template for my WordPress site. However, I think I need to alter the code to make it behave better and am not sure what to do.
The new page is at http://sassyginger.com/most-popular-posts. It shows two posts (when it's supposed to show five), and then associates them both with "zero views" which I know isn't right.
I'm very new to PHP so would appreciate any help anyone can give me on how to tweak the Wordpress popular posts without using plugins code to show five posts and leave off the incorrect 0 Views bit.
Thank you!
Wordpress don't track views on a post by default, so if you don't want to use a plugin, you need to create a custom field for all posts, which contains views. And then write a function that takes that value and adds one everything someone loads that page. ( Say you put the function in your functions.php ) and call it in from your single-template and sending the postid along.
function might look something like this:
And in our single-template we would call the function somewhere like:
Then problem number two, you can't query posts with operators, so you can't query just the five posts with highest views, so you might have to query all posts, store the views custom-field and post id in an array, then sort the array (with php's sort function). Now you got each posts ID, and that posts views in an array sorted. So take the first five (or last depending on how you sorted it) and you got the five postIDs with the highest number of views.
To sort the array:
Now you need to do a second query where you just query these IDs (with 'post__in' selector, then you can loop them out however you want.
Just note I didn't try out this code, but I've done something similar in the past. It might not be the best solution, but it will get the job done. Querying through all posts (If you have ALOT OF THEM), only to fetch five or so posts, can't be good practice :)