Count Page Hits in WordPress
by Bandicoot Marketing on | Posted in Tutorials | 7 comments
This is just a little snippet of code I wrote to count the number of hits on a specific page. I only wanted it to work on single post pages so I included the is_single() conditional tag.
function count_page_hits() {
if(is_single()) {
global $post;
$count = get_post_meta($post->ID, 'count_page_hits', true);
$newcount = $count + 1;
update_post_meta($post->ID, 'count_page_hits', $newcount);
}
}
add_action('wp_head', 'count_page_hits');
All the code does it increase the count whenever the single post page is accessed. Now you will have a custom field with the number of times the page is hit. With this data, you can start to create a list of popular posts or even a graph to show you in your dashboard which posts have received the most views on your site.
Tutorials on how to do those things will come out shortly.

7 comments for “Count Page Hits in WordPress”