Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#33223 closed defect (bug) (invalid)

add_post_meta() asign different value sometimes

Reported by: jessy-marco's profile Jessy Marco Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.2.3
Component: Options, Meta APIs Keywords:
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Similar: #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>

Change History (3)

#1 @Jessy Marco
8 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Code is broken.

Sorry about that.

#2 @ocean90
8 years ago

  • Milestone Awaiting Review deleted

#3 @SergeyBiryukov
8 years ago

  • Description modified (diff)
Note: See TracTickets for help on using tickets.