Wordpress Post View Count Different

2019-06-13 03:52发布

I been encountering the problem of post view count. The home page show the post viewed for 16 times but when I goes into (single) post. It show me 10 only. It seem some slightly delay of update in count.

I am using CloudFlare services. But this shouldn't be the cause for the problem. Suspect to be caching problem. But I have no idea how to solve it. Anyone encounter same problem like me and have some solution?

This is the source code I found in the function.php

    if ( !function_exists( 'getPostViews' ) ) {
    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. __('','color-theme-framework');
    }
}

if ( !function_exists( 'setPostViews' ) ) {
    function setPostViews($postID) {
    if (!current_user_can('administrator') ) :
        $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);
        }
    endif;
    }
}

if ( !function_exists( 'posts_column_views' ) ) {
    function posts_column_views($defaults){
        $defaults['post_views'] = __( 'Views' , 'color-theme-framework' );
        return $defaults;
    }
}

if ( !function_exists( 'posts_custom_column_views' ) ) {
    function posts_custom_column_views($column_name, $id){
        if( $column_name === 'post_views' ) {
            echo getPostViews( get_the_ID() );
        }
    }
}

add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);

标签: php wordpress
1条回答
▲ chillily
2楼-- · 2019-06-13 04:41

This is the code that I use to display post views in my website. Please note, admin views aren't counted, and if someone goes to the same post within 12 hours, the second view aren't counted. A second view count is only registered if that person visits the page again after 12 hours. So this is a much more accurate way of counting post views

if ( ! function_exists( 'pietergoosen_get_post_views' ) ) :

function pietergoosen_get_post_views($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;
}

endif;

// function to count views.
if ( ! function_exists( 'pietergoosen_update_post_views' ) ) :

function pietergoosen_update_post_views($postID) {
    if( !current_user_can('administrator') ) {
        $user_ip = $_SERVER['REMOTE_ADDR']; //retrieve the current IP address of the visitor
        $key = $user_ip . 'x' . $postID; //combine post ID & IP to form unique key
        $value = array($user_ip, $postID); // store post ID & IP as separate values (see note)
        $visited = get_transient($key); //get transient and store in variable

        //check to see if the Post ID/IP ($key) address is currently stored as a transient
        if ( false === ( $visited ) ) {

            //store the unique key, Post ID & IP address for 12 hours if it does not exist
           set_transient( $key, $value, 60*60*12 );

            // now run post views function
            $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);
            }
        }
    }   
}

endif;

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

You can now just simply add the following code where you need to display the count

<div class="readercount">
                    <?php $views = pietergoosen_get_post_views(get_the_ID());
                        if(pietergoosen_get_post_views(get_the_ID()) == 1) {  
                            printf( __( '%d Reader have read this post.', 'pietergoosen' ) , $views );
                        } else {  
                            printf( __( '%d Readers have read this post.', 'pietergoosen' ) , $views );  
                        }  
                    ?>
            </div>

and add the following code in single.php to register the count

pietergoosen_update_post_views(get_the_ID());
查看更多
登录 后发表回答