“Array Views” error in post view counter in Wordpr

2019-08-21 02:14发布

问题:

I'm newbie with PHP/Wordpress, so please help me if I'm wrong.

I've add some code to functions.php to count and then show post views. It's below:

function getPostViews($postID, $is_single = true){
   global $post;
   if(!$postID) $postID = $post->ID;
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if(!$is_single){
        return '<span class="svl_show_count_only">'.$count.' Views</span>';
    }
    $nonce = wp_create_nonce('devvn_count_post');
    if($count == "0"){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '330');
        return '<span class="svl_post_view_count" data-id="'.$postID.'" data-nonce="'.$nonce.'">0 Views</span>';
    }
    return '<span class="svl_post_view_count" data-id="'.$postID.'" data-nonce="'.$nonce.'">'.$count.' Views</span>';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count == "0" || empty($count) || !isset($count)){
        add_post_meta($postID, $count_key, '0');
        update_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
add_action( 'wp_ajax_svl-ajax-counter', 'svl_ajax_callback' );
add_action( 'wp_ajax_nopriv_svl-ajax-counter', 'svl_ajax_callback' );
function svl_ajax_callback() {
    if ( !wp_verify_nonce( $_REQUEST['nonce'], "devvn_count_post")) {
        exit();
    }
    $count = 0;
   if ( isset( $_GET['p'] ) ) {
      global $post;
      $postID = intval($_GET['p']);
      $post = get_post( $postID );
      if($post && !empty($post) && !is_wp_error($post)){
         setPostViews($post->ID);
         $count_key = 'post_views_count';
          $count = get_post_meta($postID, $count_key, true);
      }
   }
   die($count.' lượt xem bài viết');
}
add_action( 'wp_footer', 'svl_ajax_script', PHP_INT_MAX );
function svl_ajax_script() {
   if(!is_single()) return;
   ?>
   <script>
   (function($){
      $(document).ready( function() {
         $('.svl_post_view_count').each( function( i ) {
            var $id = $(this).data('id');
            var $nonce = $(this).data('nonce');
            var t = this;
            $.get('<?php echo admin_url( 'admin-ajax.php' ); ?>?action=svl-ajax-counter&nonce='+$nonce+'&p='+$id, function( html ) {
               $(t).html( html );
            });
         });
      });
   })(jQuery);
   </script>
   <?php
}
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(), false);
    }
}

And then add this to single.php:

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

It's work well until now, some post views is over ~7000 Views. That posts show me "Array Views", while others still ok.

It's hard for me to find the reason, but I'm trying. What can I do?