Our site is quite high traffic and we use both nginx and w3 total cache to handle the load. We've previously been using wp-postviews to count the page views, but it seems to be locking the postmeta table now, and often doesnt count views at all. It's unreliable at best.

Can anyone suggest a way for us to count page views and put them into the DB, or any specific workable solutions?

My initial thoughts are to have the view count done via javascript to update a separate database, then a cron job at the end of each day to merge the results, but I'm not sure where to start.

Thanks in advance

  • 1
    Am I missing something stupid here, Google Analytics will track page views. While it wont be in your database you will have complete access.
    – Cristian
    Commented Jul 8, 2011 at 8:54
  • I want it in our local database so I can start manipulating the data - a widget with the days top posts for instance. I'd also like to avoid masses of API programming ideally. Commented Jul 8, 2011 at 16:53
  • Ok, for now until I've implement a quick count php script that enters data onto it's own separate server and hence avoid locking the postmeta table. The count is called from a bit of jquery ajax, and the postviews table is going to be merged each day when traffic is lowest. I'm pretty sure this will reduce queries by a factor of thousands, and yet still give us daily accurate stats that wont be affected by caching. I need to clean it up a bit, but will post something here when finished so others can use it / abuse me for bad programming ;) Thanks to everyone who contributed so far. Commented Jul 8, 2011 at 17:03
  • James, could you please post more information on your solution?
    – Sergey
    Commented Apr 9, 2012 at 12:02
  • Actually, we had to disable my solution because it was killing the server! So, sorry, no solution. We've resorted to not using pageviews for calculated metrics, and now using social share counts instead. Commented Apr 9, 2012 at 15:28

3 Answers 3


It really depends what you need to view counts for - if it's just for seeing traffic stats, then use Google Analytics or any number of javascript tracker based analytics tools.

If you need integration of page view counts and the ability to do things like order post by views, then you can either

  • spend some time optimising your database - some options and things to consider
    • more memory for MySQL
    • change the postmeta table to be InnoDB
    • get a separate database server
    • make sure you've tuned your MySQL settings (use mysqltuner as a starting point)


  • Use something like Piwik and spend time integrating it (it has a decent API) with WordPress.
  • Yes, as you say we'd like to use it in various ways with popular post widgets etc. We also already run on a separate dedicated DB server, but we avoid innoDB as I read about lots of incompatibility issues with Wordpress. I'm not a DB expert, obviously. Will take a look at Piwik though, thanks! Commented Jul 8, 2011 at 16:54

Here's how you do it, assuming you have a memory caching extension active:

(I'm going to use APC as an example here because it will be bundled with PHP 6)

// set this to the current post id
$post_id = get_the_ID();

// this will attempt to get the post view count from cache (the memory)
$page_views = apc_fetch("post_{$post_id}_views");

// if it exists, increase the counter (again, only in memory)
if($page_views !== false){
  $page_views = apc_inc("post_{$post_id}_views");

  // write the data to the database every ...let's say 100 views
  if(($page_views % 10) == 100)

// doesn't exist; most likely this is the first view, so create the cache entry
  apc_store("post_{$post_id}_views", 1);
  $page_views = 1;

// show it
printf('This post has %d views', $page_views);

You can choose to use WP's functions - wp_cache_incr() or wp_cache_decr() + W3TC with APC selected. But personally I would avoid using any cache plugins and create my own object cache handler which I can drop in the wp-content directory.

You can also use xcache for this too, it doesn't matter - The idea is that you need to store the count in memory up to a certain point, to avoid any disk writes. Since your site has high-traffic I assume you own a dedicated server, on which you can install your own PHP extensions etc. Shared hosters won't allow you to cache data in memory for obvious reasons...

  • Interesting approach, I like it. I'm not 100% certain of the part where you set the count to 1 if it's not in memory though...shouldn't you check the DB since the memory could have lost the count value but there could be thousands of hits on an old post stored in the DB already and I believe this code would then re-set that count value on that post if it ever became missing from the memory! Unless i'm not following correctly...I like the concept though
    – JasonDavis
    Commented Dec 25, 2014 at 21:29

Just use Google Analytics or wp.com stats.

It's not worth wasting time on something that's already solved.

  • 1
    But what if i want to order posts by views ?
    – Bainternet
    Commented Jul 8, 2011 at 10:09
  • Both services allow you to do that, especially wp.com stats.
    – scribu
    Commented Jul 8, 2011 at 15:40
  • I wasn't aware wordpress.com would work with caching systems, but great idea, I'll certainly test that out! I'm not sure how I would use Google analytics to sort posts though - I guess you mean using the API and writing something custom? Commented Jul 8, 2011 at 16:49
  • Actually, I'm not sure how wordpress.stats would allow us to perform wp-queries sorted by post views, and I really want to avoid additional plugin overhead for features we dont need - pretty graphs etc. Commented Jul 8, 2011 at 17:00
  • You could do the query just like the plugin itself does it (Top Posts & Pages section). As for code bloat, you're free to take apart the plugin and leave only what you need.
    – scribu
    Commented Jul 8, 2011 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.