Custom Post Type Fields Overwritten

The past few days I’ve run up against a weird bug in a plugin I’m writing, where the custom post type fields would be overwritten with empty values randomly. Creating or editing a post would save the values fine, but I’d come back 5 minutes later and they’d be empty. I finally got it sorted, and the fix was either one or both of these two things:

1) Removing some orphaned rows from the postsmeta table that had matching meta keys, except that the case was different.

2) Adding this code to the start of my save_post callback

if( $post->post_type != 'my_post_type' || ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || $post->post_status == 'auto-draft' )
  return;

Leave a Reply

Your email address will not be published. Required fields are marked *