Writing a view count with w3 total cache

This won’t work if page caching is enabled, I would recommend creating an ajax request which calls this function instead.
e.g.

add_action( "wp_ajax_nopriv_view_count", 'view_count' );
add_action( "wp_ajax_view_count",        'view_count' );
function view_count(){
    update_post_meta($_GET['id'], 'view_count', $count++);
}

(then create an ajax call in JS which passes through the page id to this counting function)

Ajax shouldn’t be affected by page caching. There will be a slight overhead on this extra request with each pageload, but that’s the price you pay if you want to do your own pageview counting. The only way around this would be to use a third party tool like Google Analytics.

Leave a Comment