Home » WordPress » How To Jetpack Post Views Column To WordPress Posts

How To Jetpack Post Views Column To WordPress Posts

I love Jetpack’s stats module but I have a simple problem with it. I’m a former Blogger user, which shows post views in the post page. However, Jetpack shows post views on its own stats page.

Here’s a great snippet from Rich Collier’s site which adds Jetpack post views column to WordPress posts page which shows total post views on the posts screen.

Just drop the following snippet to your current theme’s functions.php file:

// Add a Views columns to insert jetpack postviews data into
function rdc_add_views_column( $cols ) {
        $cols['pageviews'] = 'Views';
  
        return $cols;
}
add_filter( 'manage_edit-post_columns', 'rdc_add_views_column' );
  
// Grab and display the postviews data from Jetpack for each post
function rdc_add_views_colurdc_data( $colname ) {
    global $post;
 
    // Make sure we're inserting into the correct column
    if ( 'pageviews' !== $colname )
        return false;
 
    // Make sure jetpack and stats are available
    if ( ! ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'stats' ) ) ) {
        echo 'Error 101';
        return false;
    }
 
    // Make sure stats_get_csv is available
    if ( ! function_exists( 'stats_get_csv' ) ) {
        echo 'Error 102';
        return false;
    }
     
    // Try to get view count from post meta "cache"
    $view_count = get_post_meta( $post->ID, '_jetpack_post_views_count', true );
    $view_count_created = absint( get_post_meta( $post->ID, '_jetpack_post_views_count_created', true ) );
     
    // No "cache" value, hit the API for the value
    if ( ! $view_count || time() > $view_count_created + 3600 ) {
        // Get the post data from Jetpack
        $postviews = stats_get_csv( 'postviews', "post_id={$post->ID}" );
 
        // We have a problem if there was no data returned
        if ( ! $postviews ) {
            echo 'Error 103';
            return false;
        }
     
        // Store the value and time as post meta
        update_post_meta( $post->ID, '_jetpack_post_views_count', absint( $postviews[0]['views'] ) );
        update_post_meta( $post->ID, '_jetpack_post_views_count_created', absint( time() ) );
    }
     
    // Print Jetpack post views
    echo '<strong>' . number_format( absint( $postviews[0]['views'] ) ) . '</strong>';
}
add_action( 'manage_posts_custom_column', 'rdc_add_views_colurdc_data' );

That’s it!

1 thought on “How To Jetpack Post Views Column To WordPress Posts”

  1. Seems that the filter changed a little bit and is now variable. Please have a look here: https://de.forums.wordpress.org/topic/views-anzeige-im-admin-bereich-fehlt?replies=12#post-535628

    It is something like manage_{$post_type}_posts_custom_column so for posts and pages one can use:

    add_action( 'manage_page_posts_custom_column', 'rdc_add_views_colurdc_data' );
    add_action( 'manage_post_posts_custom_column', 'rdc_add_views_colurdc_data' );
    

    Cheers,
    Dennis.

Leave a Comment

Scroll to Top