1

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.

EXAMPLE:

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.

2 Answers 2

3

Using save post action I check state:

<?php

add_action('save_post' ,'my_save_postdata');

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

    // logic

}
1
  • Although your answer seems to work well, I'm going to stick with my answer because i prefer to access the post data from $postarr than from actually getting the $_POST. I dont know if eitherway is actually BETTER, but I got it working how i want it, so thats what im sticking with :)
    – William
    Commented Mar 13, 2011 at 18:01
1

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:

add_filter('wp_insert_post_data',array(__CLASS__,'filter_wp_insert_post_data'),10,2);
static function filter_wp_insert_post_data($data, $postarr) {
     update_post_meta($postarr['ID'],'_offices',$postarr['offices']);
     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(isset($postarr['offices']))
update_post_meta($postarr['ID'],'_offices',$postarr['offices']);

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!

1
  • Technically, using a filter to perform an action like this is not the intention of the filters. A filter is intended to alter the data it has been passed and return the altered data for use in subsequent actions. An action should be used when an operation is being performed. In this case the 'save_post' or 'save_post_<post_type>' actions are appropriate. They are only called if all other operations related to saving the new post have succeeded.
    – KenB
    Commented Dec 30, 2013 at 16:43

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.