skip to Main Content

I have created an action with the below function and I can display the number of views of each post
But every time the page is refreshed, one view is added to this
I want the actual number of views of the post to be displayed
Thank you for your guidance

/* Counter PostViews*/
function set_post_view_custom_field() {
    if ( is_single() ) {
        global $post;
        $post_id = $post->ID;
        $count = 1;
        $post_view_count = get_post_meta( $post_id, 'post_view_count', true );
        if ( $post_view_count ) {
            $count = $post_view_count + 1;
        }
        update_post_meta( $post_id, 'post_view_count', $count );
    }
}
add_action( 'wp_head', 'wpb_track_post_views');

2

Answers


  1. Chosen as BEST ANSWER

    I worked on this function that Kilian Aziz wrote, but it doesn't work I use the following function with a little modification, but the number of visits increases by one each time the page is refreshed, and the function does not work correctly. Thank you, friends, for making a correction to my function so that I can put the number of visits correctly on WordPress to receive the correct and accurate number of a post.

    function set_post_view_custom_field() {
    if ( is_single() ) {
        global $post;
        $post_id = $post->ID;
        $count = 1;
        if ( !isset( $COOKIE['post_view_count' . $post_id] ) ) {
            $post_view_count = get_post_meta( $post_id, 'post_view_count', true );
            if ( $post_view_count ) {
                $count = $post_view_count + 1;
            }
            update_post_meta( $post_id, 'post_view_count', $count );
            setcookie( 'post_view_count' . $post_id, $post_id, time() + 3600 );
        }   
    }
    }
    add_action( 'wp_head', 'set_post_view_custom_field' );
    

    Also, I tried the following function that one of my friends wrote in another thread, and I tried with modifications on it, but it didn't work. In this function, it is checked whether the user is logged in or not, but the function still does not work and the number of visits is not added

    add_action( 'init', 'fa_setpostviews' );
    function fa_setpostviews() {
    global $post;
    
    // Do not continue if we do not have a post ID
    if ( ! isset( $post->ID ) ) {
        return;
    }
    
    // Do not continue if the user is not logged in
    if ( ! is_user_logged_in() ) {
        return;
    }
    
    // Make sure the user is on a single post.
    if ( ! is_single() ) {
        return;
    }
    
    /*
     * Note! The above if statements could be combined into one.
     */
    
    $postID = $post->ID;
    
    
        if ( is_single() ) {
        global $post;
        $post_id = $post->ID;
        $count = 1;
        if ( !isset( $COOKIE['post_view_count' . $post_id] ) ) {
            $post_view_count = get_post_meta( $post_id, 'post_view_count', true );
            if ( $post_view_count ) {
                $count = $post_view_count + 1;
            }
            update_post_meta( $post_id, 'post_view_count', $count );
            setcookie( 'wpai_visited_ip', $visit_ip_addr, time() + ( 60 * 1 ) );
    
        }   
    }
    }
    add_action( 'wp_head', 'set_post_view_custom_field' );
    

  2. You can do this using cookies. Basically set a cookie that has a lifetime of 1 hour, so that when refreshing the page, if the cookie exists, it means the user has already been on this article in the last hour :

    function set_post_view_custom_field() {
        if ( is_single() ) {
            global $post;
            $post_id = $post->ID;
            $count = 1;
            if ( !isset( $COOKIE['post_view_count' . $post_id] ) ) {
                $post_view_count = get_post_meta( $post_id, 'post_view_count', true );
                if ( $post_view_count ) {
                    $count = $post_view_count + 1;
                }
                update_post_meta( $post_id, 'post_view_count', $count );
                setcookie( 'post_view_count_' . $post_id, $post_id, time() + 3600 );
            }   
        }
    }
    add_action( 'wp_head', 'wpb_track_post_views');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search