-1

I am trying to display post view counts.

I've tried below function to display the post view count, but I'm getting same post view for all the post in loop. Not getting correct post view count.

Function:

       <?php
          // 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());
      }
       }
     ?>

Loop Code:

   <ul>
      <?php $the_query = new WP_Query( 'showposts=5' ); ?>

        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
      <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>

       <?php echo getPostViews(get_the_ID()); ?>  //post View COde

  <?php echo substr(strip_tags($post->post_content), 0, 250);?></li>

      <?php endwhile;?>
  </ul>

Need suggestions to display Correct Post View Count in while Loop

  • 1
    Whats the source of the code? – Brad Dalton May 14 '14 at 4:44
  • Can you please explain why you are deleting and then adding view count in getPostViews function? – Chittaranjan May 14 '14 at 7:12
3

Add this to single.php, and make sure you paste in inside the loop.

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

Lastly, to display the number of views a post has, just add this where you want it to be displayed:

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

.

<?php

// 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);
    }
}

?>
1

If you are storing the meta data correctly, then you can use the following code to display the count

echo (int) get_post_meta(get_the_ID(), 'post_views_count', true) . ' View(s)';
0

For the value 0 in add_post_meta the view count is not updating. Try the line add_post_meta($postID, $count_key, '1'); instead of line add_post_meta($postID, $count_key, '0');.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.