update_post_meta adds one to value
-
In the code below, I pull the page view counter from postmeta and increment it by one.
When I update via update_post_meta, the counter value that goes into the database is incremented mysteriously by 1 again. So, if the counter was at 3, after the update it will be set to 5.
If, however, I update the record directly via mysql_query, the counter value in the database is set to what I calculated, that is 1+the old counter, not 2+the old counter.
I would prefer to use update_post_meta. But why is it doing this?
Here is my code with my debugging statements. You can see the two different ways I try to update the database down in the last few lines of code. One way is commented out, because I was testing.function setPostViews($postID) { $count_key = 'Views'; $count=0; $count = get_post_meta($postID, $count_key, true); var_dump($count); echo "count " . $count . "<br>"; if($count==''){ $count = 1; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '1'); }else{ $count++; // update_post_meta($postID, $count_key, $count); $query="update hoparticlespostmeta set meta_value='".$count."' where meta_key='Views' and post_id=".$postID; mysql_query($query); } }
- The topic ‘update_post_meta adds one to value’ is closed to new replies.