php – Why Is Number of views on wordpress single post wrong?

Question

Hello wordpress devs and community. I have a concern about the number of views showing on my wordpress posts, its wrong.

I used this code in functions.php;

function gt_get_post_view() {
    $count = get_post_meta( get_the_ID(), 'post_views_count', true );
    return "$count views";
}
function gt_set_post_view() {
   $key = 'post_views_count';
    $post_id = get_the_ID();
    $count = (int) get_post_meta( $post_id, $key, true );
    $count++;
    update_post_meta( $post_id, $key, $count );
}
function gt_posts_column_views( $columns ) {
    $columns['post_views'] = 'Views';
    return $columns;
}
function gt_posts_custom_column_views( $column ) {
    if ( $column === 'post_views') {
     echo gt_get_post_view();
    }
}
add_filter( 'manage_posts_columns', 'gt_posts_column_views' );
add_action( 'manage_posts_custom_column', 'gt_posts_custom_column_views' );

Then in the content-single.php added this above

<?php gt_set_post_view(); ?>

But from my tests the views are wrong, most times when I just preview a post to check its look before publishing am seeing 4 views already.. wonder how possible.

0
user224913 1 month 2022-08-14T03:01:59-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse