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.

I have two custom meta fields that I've enabled for each post, scottb_customHeader and scottb_customTitle

These work fine as long as I'm using the full edit feature to edit posts. However, when I click "Quick Edit", then click "Update", my custom meta values for the post are cleared out. What do I need to do to resolve?

Code is below...

add_action('save_post', 'custom_add_save');


function custom_add_save($postID){
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $postID;
    }
    else
    {
        // called after a post or page is saved
        if($parent_id = wp_is_post_revision($postID))
        {
        $postID = $parent_id;
        }

        if ($_POST['scottb_customHeader']) 
        {
            update_custom_meta($postID, $_POST['scottb_customHeader'], '_scottb_customHeader');
        }
        else
        {
            update_custom_meta($postID, '', '_scottb_customHeader');
        }
        if ($_POST['scottb_customTitle']) 
        {
            update_custom_meta($postID, $_POST['scottb_customTitle'], '_scottb_customTitle');
        }
        else
        {
            update_custom_meta($postID, '', '_scottb_customTitle');
        }
    }
}

function update_custom_meta($postID, $newvalue, $field_name) {
    // To create new meta
    if(!get_post_meta($postID, $field_name)){
    add_post_meta($postID, $field_name, $newvalue);
    }else{
    // or to update existing meta
    update_post_meta($postID, $field_name, $newvalue);
    }
}
share|improve this question
    
@Scott: If you use the Quick Edit, I assume your meta fields are not displayed? In that case $_POST['scottb_customHeader'] will always be empty and you inadvertently clear them. Your custom_add_save should probably not fire when you do a quick edit? –  Jan Fabry Dec 6 '10 at 16:38
3  
Check if DOING_AJAX is defined and true - if it is, return the post ID as you do with DOING_AUTOSAVE currently. –  TheDeadMedic Dec 6 '10 at 19:00
    
@TheDeadMedic: I suspect that approach will become broken with the WP 3.1 ajaxified admin. :-D –  Denis Dec 6 '10 at 19:02
    
@Jan - correct on all counts :) –  Scott B Dec 6 '10 at 20:55
    
@Denis - say wha? Have they ajaxified the main post edit screen too now? –  TheDeadMedic Dec 6 '10 at 22:02

3 Answers 3

up vote 3 down vote accepted

Add a hidden flag to the post edit form along with your custom fields. Something like

<input type="hidden" name="my_hidden_flag" value="true" />

Then, wrap all of your custom save_post stuff in a check for this flag. Then you don't have to check for the autosave constant any more either--if the flag doesn't exist, it's either a quick edit or an autosave.

function custom_add_save($postID){

    // Only do this if our custom flag is present
    if (isset($_POST['my_hidden_flag'])) {

        // called after a post or page is saved
        if($parent_id = wp_is_post_revision($postID)) {
            $postID = $parent_id;
        }

        if ($_POST['scottb_customHeader']) {
            update_custom_meta($postID, $_POST['scottb_customHeader'], '_scottb_customHeader');
        } else {
            update_custom_meta($postID, '', '_scottb_customHeader');
        }

        if ($_POST['scottb_customTitle']) {
            update_custom_meta($postID, $_POST['scottb_customTitle'], '_scottb_customTitle');
        } else {
            update_custom_meta($postID, '', '_scottb_customTitle');
        }

    }

}
share|improve this answer
    
Just coming back to this one. Works brilliantly. Thanks MathSmath! –  Scott B Jan 13 '11 at 23:39

I had the same issue. Just add the following code at the beginning of the save_post action hook callback function (the function used to save the custom data).

// handle the case when the custom post is quick edited
// otherwise all custom meta fields are cleared out
if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
      return;

What it actually does: it checks if the quick saving wp_nonce_field exists and returns if that's the case. No need to create an additional hidden field in the form.

share|improve this answer

What happens is: The new action on 'save_post' is not equipped to handle the new data that you have in your custom fields. So when the save post runs, it's missing all the custom field $_POST data, and thus deletes the field values, leaving you with nothing.

FIX:

Where ever the add_action('save_post','your_new_action'); is, you can make it return early if you are on 'edit' screen (where the quick edits happen):

 global $current_screen;    
 if($current_screen->base == 'edit') return $post_id;

Or you can check weather the $_POST action is an inline-save (the quick edit's action name) and then not execute anything after that thus preserving your custom field values:

 if( $_POST['action'] != 'inline-save' ) { echo 'Do this then..'; }

I might have to elaborate even more if you're not sure where to put these snippets. But I hope it helps someone with similar issues.

share|improve this answer

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.