If you have a custom meta box or are working with custom metadata at the post-level, then you may stumble across a problem with having data accidentally deleted when saving it from the Bulk Edit screen.

The setup for the problem is something like this:

  1. you have a collection of posts that need to be changed,
  2. you load all posts in the bulk edit screen (as shown above),
  3. you apply a change.

Once this happens, the change occurs, but it erases any custom post metadata that you may have.

Metadata and Inline Editing

 

This is but a small code sample of what save method looks like for your custom metadata (that is, I’m leaving out a number of guard clauses):

The problem with this is there’s no check that this is coming from the Bulk Edit screen. And because of that, the metadata can be completely deleted.

To handle this, we need to check to see if the serialization is coming from the Bulk Edit screen. That way, we can return early and prevent the code from executing. In it’s simplest form, you can do something like this:

Note that I don’t recommend using this verbatim for universal cases or for universal use as it’s aggressive. Instead, I think it’s worth reading through this Codex article to get more information on how inline editing works on both the server side and the client-side.

The purpose of the post is to show how custom metadata can be deleted unintentionally and to lay the basic groundwork for how you can prevent it.