• I have a problem with Post View Count. If reads data from a list of different page than a single.php loop correctly poking 1 view. On single.php loops displays the article my count poking twice.

    Even on a single page as a single loops do nothing it does not.

    Displaying custom post type as singles.

    Where to find a solution to this error?
    in single.php

    I set the loop for custom post type

    <? php setPostViews (get_the_ID ());?>

    on the other side which is not single meter to display the results of visits to the list of articles
    <? php echo getPostViews (get_the_ID ());?>

    in another template post view count work fine.

    My Function.php (post view count)

    // function to display number of posts.
    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 View";
        }
        return $count.' Views';
    }
    
    // function to count views.
    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);
        }
    }
    
    // Add it to a column in WP-Admin
    add_filter('manage_posts_columns', 'posts_column_views');
    add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
    function posts_column_views($defaults){
        $defaults['post_views'] = __('Views');
        return $defaults;
    }
    function posts_custom_column_views($column_name, $id){
    	if($column_name === 'post_views'){
            echo getPostViews(get_the_ID());

  • The topic ‘Single page. Post View Count get twice views on single.php’ is closed to new replies.