post meta data clearing on autosave

For starters I am using this GREAT answer to achieve custom post types as well as a custom way to insert those post types into other pages, you can view that answer here:
Custom field/meta populated by dropdown of existing posts?

I am running into a problem however when wordpress auto saves itself, it clears whatever metadata i have put into the forms.


I select the 3 drop downs I want from my metabox and hit update.

The page reloads and the 3 choices are still selected. In phpMyAdmin i can look up the metadata and see the array in place as it should be.

I now wait for wordpress to auto save.

As soon as this happens i can either refresh the page (not update, just refresh) and the metadata is lost, or i can look up the metadata in phpMyAdmin and it will display nothing (ie: its gone).

I’m guessing that this has something to do with needing to tie into another hook (some sorta auto update hook) or something, but even then it doesn’t make sense that it would actually delete your metadata (rather than just not auto updating it).

Last note:
If you copy and paste Mikes code into functions.php, you should be able to recreate my error (in wordpress 3.1) very easy.

Solutions Collecting From Web of "post meta data clearing on autosave"

Using save post action I check state:


add_action('save_post' ,'my_save_postdata');

function my_save_postdata ( $post_id )
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
        return $post_id;

    // logic


Well, as always I feel like a dummy, but after searching the internet for hours and seeing that it seems like lots of people are having this question and I cant seem to find an an answer anywhere, I guess im not THAT slow!

the important code is something along these lines:

static function filter_wp_insert_post_data($data, $postarr) {
     return $data;

what was happening is this code gets run, but there is no $postarr[‘offices’], so it inserts blank data instead! so the quick solution to this is:


If the data you want to insert isn’t set, it wont update!

I’m not sure why wordpress doesn’t pass these variables in on an auto save but it does on the real save, but either way this works so I’m happy!