“Quick Edit” > update clears out my custom meta values

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?

Read More

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);
    }
}

Related posts

Leave a Reply

5 comments

  1. 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');
            }
    
        }
    
    }
    
  2. 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.

  3. The question is quite old, but I think that problem stil exists.

    To be honest there is no good answer here because there is nothing about security. You save data to database without checking their source…

    You should use nonce to verify the source of data – thanks to this they wont be overwrite (nor delete) by QuickEdit. WordPress give you utils for this

    Add to your form:

    wp_nonce_field('my_custom_page', '_my_custom_page');
    

    And than at the beggining of your save function:

    if (!wp_verify_nonce( $_POST['_my_custom_page'], 'my_custom_page' )) { return $post_id; }
    

    You should do this for each custom post.

  4. 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.

  5. The problem here is that WP sees that your fields are not set, and you are telling WP to clear them out. Don’t clear it out if the field doesn’t exist. Clear it out if the field exists, but is empty.

        if ( isset( $_POST[$meta_key] ) ){
            $meta_value = $_POST[$meta_key];
            if ($meta_value) {
                update_term_meta($term_id, $meta_key, $meta_value);
            } else {
                delete_term_meta($term_id, $meta_key);
            }
        }