How to unlink all posts from tracking same amount of views

I'm trying to show the top 5 most popular ( most viewed ) blogs. It is working as it should and then I added a script in the functions.php to display the views just while I build this so I can see it's working.

The problem is when I click a post, they all +1 in views, even tho they are ordering my most viewed to least viewed as they should, they are still connected to each other. How do I get them to ++ independently and not be connected?

functions.php

  /* most popular script to count the 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';
    }

    /* the script to display the view count */

    function bac_PostViews($postID) {

        //Set the name of the Posts Custom Field.
        $count_key = 'post_views_count';

        //Returns values of the custom field with the specified key from the specified post.
        $count = get_post_meta($postID, $count_key, true);

        //If the the Post Custom Field value is empty.
        if($count == ''){
            $count = 0; // set the counter to zero.

            //Delete all custom fields with the specified key from the specified post.
            delete_post_meta($postID, $count_key);

            //Add a custom (meta) field (Name/value)to the specified post.
            add_post_meta($postID, $count_key, '0');
            return $count . ' View';

        //If the the Post Custom Field value is NOT empty.
        }else{
            $count++; //increment the counter by 1.
            //Update the value of an existing meta key (custom field) for the specified post.
            update_post_meta($postID, $count_key, $count);

            //If statement, is just to have the singular form 'View' for the value '1'
            if($count == '1'){
            return $count . ' View';
            }
            //In all other cases return (count) Views
            else {
            return $count . ' Views';
            }
        }
    }

the loop

 ?php $catquery = new WP_Query(array(
          'meta_key' = 'post_views_count',
          'orderby' = 'meta_value_num',
          'posts_per_page' = 5,
          'cat' = '52',

        )); ?

      ol class="topheadlines"
      ?php while($catquery-have_posts()) : $catquery-the_post(); ?
        !-- display view count -- 
        ?php if(function_exists('bac_PostViews')) {
            echo bac_PostViews(get_the_ID());
        }?
      div class="row"
        div class="col-9" style="padding:0px 0px 0px 5px;"
          li class="topheadlines-text"a href="?php the_permalink() ?" rel="bookmark"?php the_title(); ?/a/li
        /div
        div class="col-3 topheadline-thumb" style="padding:0px 5px 0px 5px;"
          ?php echo get_the_post_thumbnail( $post-ID, 'large' ); ?
        /div
      /div

      ?php endwhile;
          wp_reset_postdata();
      ?

single.php

?php
/**
 * The template for displaying all single posts
 *
 * @package understrap
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

get_header();
$container = get_theme_mod( 'understrap_container_type' );
?


  div class="row" style="margin:0;"
    div class="col-md-9 single-bg" style="padding:0;"
        div class="container-fluid"
            div class="single-wrap"

                h5 class="single-date"
                    ?php echo get_the_date(); ?
                /h5

                h2 class="single-title"
                    ?php the_title(); ?
                /h2

                h3 class="single-sub-title"
                    ?php echo the_field('mt_post_subtitle'); ?
                /h3

                ?php if (have_posts()) : while (have_posts()) : the_post(); ?
                div class="single-thumb"

        ?php $category = get_the_category();
          $firstCategory = $category[0]-cat_name;
        ?

        ?php if($firstCategory != 'Daily (Email)') { ?
             ?php echo get_the_post_thumbnail( $post-ID, 'large' );

          }
        ?

                /div

                div class="single-content"
                    ?php the_content(); ?
                /div

                  ?php
                    $author_id = get_the_author_meta('ID');
                    $author_badge = get_field('author_badge', 'user_'. $author_id );
                    ?

                div class="author-wrap clearfix"
                    div class="author-pic"
                        a href="?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?" title="?php echo esc_attr( get_the_author() ); ?"
                        ?php echo get_avatar( get_the_author_meta( 'ID' ) , 50 ); ?
                    /a
                    /div
                    h5 class="author-name"
                        a href="?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?" title="?php echo esc_attr( get_the_author() ); ?"
                        ?php the_author(); ?brspan class="author-title"?php the_field('title', 'user_'. $author_id ); ?, The Hustle/span
                    /a
                    /h5
                /div!-- end author wrap --



                ?php endwhile; ?
                ?php endif; ?

                ?php $category = get_the_category();
                    $firstCategory = $category[0]-cat_name;
                ?


                ?php if($firstCategory == 'Daily (Email)') { ?
                    a href="/daily"
                        button type="button" class="btn btn-primary single-read-more"?php echo $button_text; ?Related Posts/button
                    /a
                    ?php
                    }
                ?
                ?php if($firstCategory == 'Stories') { ?
                    a href="/stories"
                        button type="button" class="btn btn-primary single-read-more"?php echo $button_text; ?Related Posts/button
                    /a
                    ?php
                    }
                ?

                ?php if($firstCategory == 'Blog') { ?
                    a href="/blog"
                        button type="button" class="btn btn-primary single-read-more"?php echo $button_text; ?Related Posts/button
                    /a
                    ?php
                    }
                ?

                ?php if($firstCategory == 'Episodes') { ?
                    a href="/"
                        button type="button" class="btn btn-primary single-read-more"?php echo $button_text; ?Related Posts/button
                    /a
                    ?php
                    }
                ?
                ?php if($firstCategory == '') { ?
                    a href="/"
                        button type="button" class="btn btn-primary single-read-more"?php echo $button_text; ?Related Posts/button
                    /a
                    ?php
                    }
                ?
                ?php if($firstCategory == 'Uncategorized') { ?
                    a href="/"
                        button type="button" class="btn btn-primary single-read-more"?php echo $button_text; ?Related Posts/button
                    /a
                    ?php
                    }
                ?
        ?php if($firstCategory == 'Brief') { ?
          a href="/series/brief"
            button type="button" class="btn btn-primary single-read-more"?php echo $button_text; ?Related Posts/button
          /a
          ?php
          }
        ?

            div class="recent-wrap"
                h4 class="featured-headlines"
                  Recent Posts
                /h4
                ?php echo do_shortcode('[recent_post_carousel design="design-1" category="52,6"]'); ?

            /div!-- end recent wrap --

            /div!-- end singlewrap --

        /div!-- end container fluid --
    /div!-- end col 9 --
    div class="col-md-3" style="padding:0; margin:0;"
      ?php include get_template_directory() . '/inc/page-sidebar.php'; ?
    /div!-- end col 3 sidebar --
  /div !-- end master row --


?php get_footer();

Topic views loop php Wordpress

Category Web


Use this in the loop:

getPostViews(get_the_ID());

instead of:

bac_PostViews(get_the_ID());

So your loop should be like this:

<?php $catquery = new WP_Query(array(
          'meta_key' => 'post_views_count',
          'orderby' => 'meta_value_num',
          'posts_per_page' => 5,
          'cat' => '52',

        )); ?>

      <ol class="topheadlines">
      <?php while($catquery->have_posts()) : $catquery->the_post(); ?>
        <!-- display view count --> 
        <?php if(function_exists('getPostViews')) {
            echo getPostViews(get_the_ID());
        }?>
      <div class="row">
        <div class="col-9" style="padding:0px 0px 0px 5px;">
          <li class="topheadlines-text"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
        </div>
        <div class="col-3 topheadline-thumb" style="padding:0px 5px 0px 5px;">
          <?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>
        </div>
      </div>

      <?php endwhile;
          wp_reset_postdata();
      ?>

And in single.php you must call bac_PostViews function:

<?php
/**
 * The template for displaying all single posts
 *
 * @package understrap
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

get_header();
$container = get_theme_mod( 'understrap_container_type' );
?>


  <div class="row" style="margin:0;">
    <div class="col-md-9 single-bg" style="padding:0;">
        <div class="container-fluid">
            <div class="single-wrap">

                <h5 class="single-date">
                    <?php echo get_the_date(); ?>
                </h5>

                <h2 class="single-title">
                    <?php the_title(); ?>
                </h2>

                <h3 class="single-sub-title">
                    <?php echo the_field('mt_post_subtitle'); ?>
                </h3>

                <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                <div class="single-thumb">

        <?php if(function_exists('bac_PostViews')) {
            echo bac_PostViews(get_the_ID());
        }?>

        <?php $category = get_the_category();
          $firstCategory = $category[0]->cat_name;
        ?>

        <?php if($firstCategory != 'Daily (Email)') { ?>
             <?php echo get_the_post_thumbnail( $post->ID, 'large' );

          }
        ?>

                </div>

                <div class="single-content">
                    <?php the_content(); ?>
                </div>

                  <?php
                    $author_id = get_the_author_meta('ID');
                    $author_badge = get_field('author_badge', 'user_'. $author_id );
                    ?>

                <div class="author-wrap clearfix">
                    <div class="author-pic">
                        <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" title="<?php echo esc_attr( get_the_author() ); ?>">
                        <?php echo get_avatar( get_the_author_meta( 'ID' ) , 50 ); ?>
                    </a>
                    </div>
                    <h5 class="author-name">
                        <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" title="<?php echo esc_attr( get_the_author() ); ?>">
                        <?php the_author(); ?><br><span class="author-title"><?php the_field('title', 'user_'. $author_id ); ?>, The Hustle</span>
                    </a>
                    </h5>
                </div><!-- end author wrap -->



                <?php endwhile; ?>
                <?php endif; ?>

                <?php $category = get_the_category();
                    $firstCategory = $category[0]->cat_name;
                ?>


                <?php if($firstCategory == 'Daily (Email)') { ?>
                    <a href="/daily">
                        <button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
                    </a>
                    <?php
                    }
                ?>
                <?php if($firstCategory == 'Stories') { ?>
                    <a href="/stories">
                        <button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
                    </a>
                    <?php
                    }
                ?>

                <?php if($firstCategory == 'Blog') { ?>
                    <a href="/blog">
                        <button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
                    </a>
                    <?php
                    }
                ?>

                <?php if($firstCategory == 'Episodes') { ?>
                    <a href="/">
                        <button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
                    </a>
                    <?php
                    }
                ?>
                <?php if($firstCategory == '') { ?>
                    <a href="/">
                        <button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
                    </a>
                    <?php
                    }
                ?>
                <?php if($firstCategory == 'Uncategorized') { ?>
                    <a href="/">
                        <button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
                    </a>
                    <?php
                    }
                ?>
        <?php if($firstCategory == 'Brief') { ?>
          <a href="/series/brief">
            <button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
          </a>
          <?php
          }
        ?>

            <div class="recent-wrap">
                <h4 class="featured-headlines">
                  Recent Posts
                </h4>
                <?php echo do_shortcode('[recent_post_carousel design="design-1" category="52,6"]'); ?>

            </div><!-- end recent wrap -->

            </div><!-- end singlewrap -->

        </div><!-- end container fluid -->
    </div><!-- end col 9 -->
    <div class="col-md-3" style="padding:0; margin:0;">
      <?php include get_template_directory() . '/inc/page-sidebar.php'; ?>
    </div><!-- end col 3 sidebar -->
  </div> <!-- end master row -->


<?php get_footer();

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.