WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there anyway to show in a template for a post, how many times that post has been viewed, like hits without some sort of plugin? It seems wordpress would have this built in, but I am unable to find any template tags for such a think in the documentation?

share|improve this question
up vote 3 down vote accepted

Not in core. You can use post meta to store this information on page loads. If you have caching plugins enabled, you might want to increment the hit counter with an AJAX call, otherwise you can just add this directly in your single.php and page.php templates:

//Add to functions.php
function get_hits(){
    global $post;
    $hits = get_post_meta($post->ID, '_hit-counter', true);
    return $hits;

function update_hits($count){
    global $post;
    $count = $count ? $count : 0;
    $hits = update_post_meta($post->ID, '_hit-counter', $count++);
    return $hits;

//Usage within the loop
share|improve this answer
Brian - I'm struggling to get this to work. Presumably I add the two functions to functions.php and use the updates_hits(get_hits()) in the page template inside the loop. What do I use to echo out the value to screen? get_hits()? – TomC Dec 12 '14 at 17:03
@TomC This solution was something I created before understanding race conditions. I wouldn't actually use this now. I would create a hits table then log each hit with a relational column for post_id. – Brian Fegter Dec 12 '14 at 19:44

No, WP doesn't have such functionality built-in. Probably because updating the post table on each page hit would slow down the site.

You'll have to use wp-postviews or something related.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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