Displaying the Number of Visits in a WordPress Blog Post

April 22, 2010 | By

I thought it would be a good idea to show how many visits a blog post had since it was published. One of the web tracking tools I use is WordPress.com Stats, and it’s the easiest to get the page view count from. So after some googling I found the best way to do so is to use the WordPress.com Stats Helper plugin and two lines of PHP code to display the number.

Being the most popular web tracking tool for WordPress, WordPress.com Stats needs no introduction. And WordPress.com Stats Helper is a plugin developed by Vlad Bailescu that allows you to retrieve data from WordPress.com Stats. The plugin was not updated for a long time, since November 2008, but it still does the job.

I placed the code below in the single.php page of the current theme. Which is the page used to display a single post.

The function_exists function checks the list of built-in and user-defined functions, and returns TRUE if it’s found. I used it to make sure the function wpcomstats_visits, which is provided by the plugin, is available and no error is displayed to the user if the plugin is disabled or removed.

The wpcomstats_visits function shows the views count and takes the following 4 arguments: the text to display before the views count, the text to display after the views count, the id of the post or page and the number of days to return the stats of. To get the id of the current post or page you just pass the WordPress function get_the_ID in the third argument.

I hope you find this helpful.

Filed in: PHP, WordPress | Tags: ,

Comments (9)

  1. Hey,

    Wondering if you can use this to call out visits on a particular day? Eg, post visits today, and for the last 7 days so it would return something like “7,8,7,8,9,4,3” << those are visits per day for 7 days


  2. @Dave, the WordPress.com Stats Helper plugin is limited to returning the number of visit to date. So setting the last argument to 0, 1, 7 or 30 will return all, today’s, last 7 days or 30 days respectively. And these are the only valid values.

  3. Yeah had a little play last night and couldn’t hack it to do what I wanted.


  4. This method works like a charm! Truly appreciate it! =)

  5. EO

    this was hugely helpful! thank you for posting…

    I also have a question – it seems that the visit count has been stuck on a particular value for hours after I initially added the php code in my theme’s footer, despite many more visits to the site. I’m running WP 3.2.1, might there a compatibility issue or maybe something in need of modification in the code?

    thanks for your help!


  6. @EO you’re welcome. I tried the code now for a few minutes and it worked. Did you make sure the 3rd argument (the page ID) has the correct value?

  7. EO

    ah yes, this is where the problem is –

    If I wanted to show only the total views of my entire site, the plugin-page says to specify “null” to display the count for all posts/pages… what then, is the correct syntax for the page ID?

    thank you once again! 🙂

  8. @EO you can use the get_the_ID() wordpress function to get the current page/post ID. But it must be called within the wordpress Loop (http://codex.wordpress.org/Function_Reference/get_the_ID for more info). If you want to call it outside the loop you’ll have to assign get_the_ID()’s returned value to a variable and use it later.

  9. EO

    YES! got it working – to display all blog visits outside of the wordpress loop, simply omit the “get_the_ID(),” entirely from the code, for example:

    thanks for pointing me in the right direction!