Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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!

share|improve this answer
    
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 Dec 30 '13 at 16:43

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

}
share|improve this answer
    
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 Mar 13 '11 at 18:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.