0

I created a plugin that has multiple meta boxes. One meta box saves a single record by using update_post_meta(). Another meta box saves multiple record by using add_post_meta().

I do not have revisions turned on in the post_type.

How would you delete_post_meta() a single specific record in the meta box that uses add_post_meta()? Best practice advise is needed.

If you're going to send the whole $post->ID and it will get the array of records, how can I detect the specific meta_key set to be deleted?

Here is the code on github: https://github.com/fritzdenim/stec-events/blob/62d5c641622f75a610da09462260f09713f0abab/index.php

1 Answer 1

1

I'd say there is barely enough information in the question to speculate on an answer but it sounds like to me that...

If your data is saved in individual key/value pairs then delete_post_meta will do it. You would pass the function a post ID and a key name: delete_post_meta(1,'keyname');. It will delete the whole key/value pair.

If you have multiple bits of information saved as a serialized array/object then delete_post_meta will not work. You will need to pull the data for the key you want with get_post_meta, alter the array/object in PHP to remove the key/value you want to delete, and then save the altered data with update_post_meta.

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

7
  • Where would the delete_post_meta() get the $post_id from? I'll place the code anyway... just a sec.
    – Franz Noel
    Commented Jun 28, 2013 at 17:43
  • Your description makes me think delete_post_meta won't work, but the short answer to that question is that you have to give the post ID to the function.
    – s_ha_dum
    Commented Jun 28, 2013 at 17:44
  • If you're going to send the whole $post->ID and it will get the array of records, how can I detect the specific meta_key set to be deleted?
    – Franz Noel
    Commented Jun 28, 2013 at 17:58
  • You also give delete_post_meta a key but take a careful look at the answer. delete_post_meta will delete the entire key. That is why I said delete_post_meta will not work. You need to pull the data, process it in PHP and then put the data back.
    – s_ha_dum
    Commented Jun 28, 2013 at 18:07
  • I tried this and it does not work because there are 2 hooks for publish_post and save_post. Maybe this will work in a json setting where you update one field.
    – Franz Noel
    Commented Jun 28, 2013 at 21:20

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.