Custom field being erased after autosave

I use the following code to prevent updates of my custom fields during auto-saves, ajax requests (quick edit) and bulk edits.

add_action('save_post', 'save_my_post');
function save_my_post($post_id)
{
    // Stop WP from clearing custom fields on autosave,
    // and also during ajax requests (e.g. quick edit) and bulk edits.
    if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (defined('DOING_AJAX') && DOING_AJAX) || isset($_REQUEST['bulk_edit']))
        return;

    // Clean, validate and save custom fields
}

Also see: How to prevent custom fields from being cleared during a bulk edit?

Leave a Comment