WordPress.org

Forums

number of postviews not change in Main page (2 posts)

  1. Arioman
    Member
    Posted 1 year ago #

    hi , i have strange problem
    i use these codes to show number of post views

    in function.php :

    <?php
    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 "بدون بازدید";
        }
        return $count.' بازدید';
    }
    
    // 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);
        }
    }
    ?>

    and in single.php :

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

    and in my index.php :
    <?php echo getPostViews(get_the_ID()); ?>

    the problem is the views counter change and increase when i go to post ( read more ...) but when i came back to my home page the counter roll back to previews number and nothing changed !!!

    whats the problem ?!?

    and another strong thing is when i login as admin the real number of post views showed !! and everything works fine .

    it seems the problem is with Guest users or something like that
    [Do not post code using <strong> but use the code button or backticks. Your code may have been damaged by the forum's parser]

  2. Arioman
    Member
    Posted 1 year ago #

    any help ??

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.