A
A
Aligatro2014-02-15 17:14:17
PHP
Aligatro, 2014-02-15 17:14:17

How to count views in Wordpress using ajax?

Good day. After installing the cache, a certain problem arose. When caching is enabled (for obvious reasons), views of articles on the site are not counted. The counting of views itself is performed by 2 functions:

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0";
    }
    return $count.'';
}
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);
    }
}


I would like to ask knowledgeable, versed people in ajax, to help make a script for passing the value of the view (bypassing caching) after refreshing the page.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan, 2014-02-15
@rOOse

What caching plugin? Read its documentation, there is usually a way to specify what not to cache.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question