Answer the question
In order to leave comments, you need to log in
W3 Total Cache and view count - how to do it?
Hi
I use W3Total Cache for caching, about 2-3 thousand people come to the site in 10 minutes (in peaks, 5-6 times a day)
Everything works great with this plugin, there are no lags. But!
Used function
function setPostViews($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);
}
}
Answer the question
In order to leave comments, you need to log in
Solved with Ajax
<script type="text/javascript">
jQuery.ajax({
type:'get',
url:'wp-count.php',
data:{'id':'<? echo get_the_ID(); ?>'},
response:'text',
success:function (data) {
console.log(data);
}
});
</script>
<?
/** Sets up the WordPress Environment. */
require( dirname(__FILE__) . '/wp-load.php' );
$postID = $_GET['id'];
echo $postID;
if($postID>0) {
$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');
echo 'add_0';
} else {
$count++;
update_post_meta($postID, $count_key, $count);
echo 'add';
}
}
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question