0

i am trying to update only one custom field that is store in serialized form. By updating that field my other custom meta fields becomes blank/empty.

how can i update only a specific meta value inside the serialized data.

update_post_meta( $pId, 'my_meta', $new_data );

Many Thanks,

2

It sounds like you are trying to directly save the altered information to the serialized value in the database. You can't do that.

You have to extract the current meta data for that key so that you have and array or object, update that array or object, and then save the whole thing back to the database.

Something like...

$old_meta = get_post_meta( $pId, 'my_meta', true);
$old_meta['somekey'] = $new_value;
update_post_meta( $pId, 'my_meta', $old_meta );

Note: get_post_meta will unserialize for you.

Also be aware that update_post_meta takes a fourth parameter...

update_post_meta($post_id, $meta_key, $meta_value, $prev_value);

That parameter is optional but if used is intended to...

... differentiate between several fields with the same key. If omitted, and there are multiple rows for this post and meta key, all meta values will be updated.

http://codex.wordpress.org/Function_Reference/update_post_meta

You can use that to make sure you only overwriting the key/value you intend to overwrite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.