I want to delete an element (post id) from a serialized array associated with a specific meta_key. The following screenshot tells the whole story.

enter image description here

What you're seeing are, post ids saved in a serialized array. Upon deletion of any post the id of the post should be removed from meta_value where meta_key is "whatever". I want to wrap unset for elements (both key and value) inside a function as shown below. Sounds complex to me.

add_action('before_delete_post', 'upon_deleting_post');
function upon_deleting_post($post_id){

1 Answer 1


Don't focus on serialized aspect of it.

  1. Retrieve data using appropriate API function.
  2. Modify the data.
  3. Save data back.

WordPress APIs take care of serialization transparently, no need to mess with it directly.

  • You mean like this?? add_action('before_delete_post', 'upon_deleting_post'); function upon_deleting_post($post_id){ delete_metadata ( $user_meta, $object_id, 'whatever', '$post_id' = '', $delete_all = true ) }; But what I do with $object_id
    – wp student
    Jan 11, 2014 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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