[wp-trac] [WordPress Trac] #33223: add_post_meta() asign different value sometimes

WordPress Trac noreply at wordpress.org
Fri Jul 31 20:55:53 UTC 2015


#33223: add_post_meta() asign different value sometimes
--------------------------------+-----------------------------
 Reporter:  Jessy Marco         |      Owner:
     Type:  defect (bug)        |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Options, Meta APIs  |    Version:  4.2.3
 Severity:  normal              |   Keywords:
  Focuses:                      |
--------------------------------+-----------------------------
 Similar: [https://core.trac.wordpress.org/ticket/32290]

 In Firefox, and Twenty-Fifteen theme it doesn't work also.


 {{{
 function oi_get_most_viewed_post(){
     global $wpdb;
         $query   = "SELECT max(cast(meta_value as unsigned)) FROM
 wp_postmeta WHERE meta_key='_post_views_count'";
         $the_max = $wpdb->get_var($query);
     return $the_max;
 }


 function oi_percentage_post_views($postID) {

         $count_key = '_post_views_count';
     $post_views = get_post_meta($postID, $count_key, true);
         $most_viewed_post = oi_get_most_viewed_post(get_the_ID());

         return $percentage = round(((( $post_views - $most_viewed_post ) /
 $most_viewed_post) * 100 ) + 100);
 }

 function oi_get_post_views($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;
 }


 function oi_set_post_views($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);
     }
 }
 }}}

 place tags into single.php and refresh the page more times, wait 1 minute
 and refresh again. Count is incremented more than one. sometimes 8,
 sometimes, 2, sometimes 13.

 The code working fine.

 Tags:

 {{{
 <div><strong>Post Views: </strong><?php oi_set_post_views(get_the_ID());
 echo oi_get_post_views(get_the_ID()); ?></div>
         <div><strong>Post Views Percentge: </strong><?php echo
 oi_percentage_post_views(get_the_ID()); ?></div>
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/33223>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list