Just wondering what the best way to save postmeta is.
I don't want empty postmeta values clogging up the database.
I had already been saving postmeta in my plugin, which was adding empty values, so I'd like to clean those up as people re-save their posts.
Any opinions on doing it like this? Improvements?:
if ( isset( $_POST[ '_my_meta_key' ] ) && $_POST[ '_my_meta_key' ] !== '' ) {
update_post_meta( $post_id, '_my_meta_key', $_POST[ '_my_meta_key' ] );
} else {
delete_post_meta( $post_id, '_my_meta_key' );
}
get_post_meta()
and output it in input? If you did it the wrong way and if input is empty every time you'll update the post but there's a value in database, you'll lose it. Otherwise your code works just fine. PS! We have no means to test it in your environment. Use this code with one input, make a dummy post, try to update it and see what happens in your database.