Leave a Reply

2 comments

  1. 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
    
    }
    
  2. 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!