Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to develop functionality which counts how many times a post is viewed however I am also using page caching (APC possible memcache in the future) via w3 total cache.

The "view count" value is not to be shown to anyone but administrators/editors and only when editing the post in admin section.

So my question is if I do the following for every post

update_post_meta($postID, 'view_count', $count++);

is it going to

  • work considering im using page caching?

  • if it does work, would it slow down the request considerably?

  • if it does slow down the request would doing a asynchronous mysql update via MYSQLI_ASYNC speed this back up?

Thanks

share|improve this question

1 Answer 1

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.

share|improve this answer
    
Can you please demonstrate how to create an ajax call in JS that passes the page ID to the counting function? –  Christine Cooper Jul 8 at 17:33

Your Answer

 
discard

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.