1+1 php counter inside the update_post_meta

how do you create a 1+1 php counter inside the update_post_meta?

this is the code i have so far:

$count = 1;
update_post_meta($bid_id, 'draftnumber', $count++);

but the counter is not moving, so i tried another one:

$counting = $counting + 1;
update_post_meta($bid_id, 'draftnumber', $counting);

but it’s not working, am i not allowed to use $count++ inside the update_post_meta?

thankyou

1 Answer
1

If you’re trying to update the number stored in the meta table, you need to load that value and increment it.

$count = get_post_meta( $bid_id, 'draftnumber', true );
if ( ! $count ) {
    $count = 0;  // if the meta value isn't set, use 0 as a default
                 // We'll be incrementing this right away so it'll be 1
}
$count++;
update_post_meta( $bid_id, 'draftnumber', $count );

References

  • get_post_meta()
  • update_post_meta()

Leave a Comment