Y
Y
yantotal2015-05-03 02:39:30
PHP
yantotal, 2015-05-03 02:39:30

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);
    }

}

And it doesn't work when caching is enabled. What are the ways to get around this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
yantotal, 2015-05-03
@yantotal

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 question

Ask a Question

731 491 924 answers to any question