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

5 s
5

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

    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *