How to get view count of every page on site and add that (increasing) number to Dashboard Widget

I don’t really have any code, but I’m trying to get the page views of every page on a WordPress site (and every time I hit refresh, the number of page views should increase). I would also like it so I could output that increasing number into a dashboard widget. A bit of complex code I would imagine, but I have no idea where to even start with this.

Solutions Collecting From Web of "How to get view count of every page on site and add that (increasing) number to Dashboard Widget"

You can generate page views count without any plugins. What you need to do is create a post meta key for each post (of course automatically) and increase the counter of this post meta key on each post/page load or visit.

So here is the function to create a post meta key post_views_count for each post and calling this function on each page/post load will increase post count by one.

// Post views function
function wps_set_post_views( $postID ) {
    $count_key = 'post_views_count';
    $count = get_post_meta( $postID, $count_key, true );
    if ( $count=='' ){
        $count = 0;
        delete_post_meta( $postID, $count_key );
        add_post_meta( $postID, $count_key, '0' );
    } else {
        $count++;
        update_post_meta( $postID, $count_key, $count );
    }
}

Now you need to call this function from single.php and page.php. Just paste this on these pages. If you use W3 Total Cache or similar cache plugin then you will need to use fragment caching to make sure function is called on each page load.

<?php wps_post_views( get_the_ID() ); ?>

This will set you post/page views and increase counter by one on each visit.

Now if you want to add post views on admin column then you can add a column for post views like this.

// Get post views
function wps_get_post_views( $postID ) {
    $count_key = 'post_views_count';
    $count = get_post_meta( $postID, $count_key, true );
    if ( $count=='' ){
        delete_post_meta( $postID, $count_key );
        add_post_meta( $postID, $count_key, '0' );
        return "0 View";
    }
    return $count.' Views';
}

// Add to admin post column
add_filter( 'manage_posts_columns', 'posts_column_views' );
add_action( 'manage_posts_custom_column', 'posts_custom_column_views', 5, 2 );
function posts_column_views( $defaults ) {
    $defaults['post_views'] = __('Views');
    return $defaults;
}
function posts_custom_column_views( $column_name, $id ){
  if ( $column_name === 'post_views' ) {
        echo wps_get_post_views( get_the_ID() );
    }
}

That’s it. All these functions will go in functions.php file except the code for single.php and page.php to call function to set post views.

There are many plugins out there showing the site’s statistics along with the popular posts/pages with their reading count.

  • Jetpack by WordPress.com – has a nice site statistics module

And there are others too…

  • Google search result
  • WordPress plugin repo search result

Most of them even provide a short stat view dashboard widget from where you can see the basic information too.