How to manage saving custom field from Quick edit and Post Save using save_post action hook without colliding each other?

I’s following Rachel Carden’s tutorial on creating Quick Edit and Bulk Edit, and grabbed her full work from her github repo. I managed doing everything fine and I made a checkbox instead of radio buttons for one of my field called “Featured” (meta_key: pre_featured).

I made some changes to the concept though? As I’s using a checkbox instead of Radio Buttons I had only one state: “checked ? grab the value ‘yes’ : else null”. So I made the update_meta part differently:

foreach( $custom_fields as $q_field ) {            
    if ( array_key_exists( $q_field, $_POST ) ) {
        update_post_meta( $post_id, $project_prefix . $q_field, $_POST[ $q_field ] );
    } else {
        delete_post_meta( $post_id, $project_prefix . $q_field ); //delete if not checked
    }
}

It’s working awesomely good.

But now when I’s implementing the meta field update in New Post page or Update Post Page, something odd happened. I’m using Tammy Hart Reusable Custom Meta Tutorial for saving meta fields value while saving in post.php or post-new.php. And found, in both the cases the custom fields are saving using 'save_post' action hook.

Whenever I’m saving my Post, two meta fields with same name getting override, like-
Post page is taking pre_featured a value: yes, and saving it, and again, the else part of that quick edit function is deleting it again.

I can’t understand why it’s happening, both the function have individual nonce checker and should work individually. But they are colliding. 🙁

Post Save function:

function save_cpt_specifics_meta( $post_id ) {
    $project_prefix = 'pre_';
    $cpt_specifics_fields = get_cpt_specifics( $post_id, $project_prefix ); //taking my fields that's working for other fields undoubtedly

    // verify nonce
    if (!isset($_POST['cpt_nonce']) || !wp_verify_nonce($_POST['cpt_nonce'], basename(__FILE__)))
        return $post_id;
    // check autosave
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return $post_id;
    // check permissions
    if ( 'offers' == $_POST['post_type'] ) {
        if ( !current_user_can('edit_page', $post_id) )
            return $post_id;
        } elseif ( !current_user_can('edit_post', $post_id) ) {
            return $post_id;
    }

    // loop through fields and save the data
    foreach ( $cpt_specifics_fields as $field ) {
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];
        if ( $new && $new != $old ) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ( '' == $new && $old ) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    } // end foreach
}

add_action('save_post', 'save_cpt_specifics_meta', 10);
add_action('new_to_publish', 'save_cpt_specifics_meta', 10);

Quick Edit Save function:

function project_save_quick_edit_featured_data( $post_id, $post ) {
    $project_prefix = 'pre_';

    // pointless if $_POST is empty (this happens on bulk edit)
    if ( empty( $_POST ) )
        return $post_id;

    // verify quick edit nonce
    if ( isset( $_POST[ '_inline_edit' ] ) && ! wp_verify_nonce( $_POST[ '_inline_edit' ], 'inlineeditnonce' ) )
        return $post_id;

    // don't save for autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    // dont save for revisions
    if ( isset( $post->post_type ) && $post->post_type == 'revision' )
        return $post_id;

    switch( $post->post_type ) {    
        case 'cpt':        
            /**
             * Because this action is run in several places, checking for the array key
             * keeps WordPress from editing data that wasn't in the form, i.e. if you had
             * this post meta on your "Quick Edit" but didn't have it on the "Edit Post" screen.
             */
            $custom_fields = array( 'featured' );

            foreach( $custom_fields as $q_field ) {            
                if ( array_key_exists( $q_field, $_POST ) ) {
                    update_post_meta( $post_id, $project_prefix . $q_field, $_POST[ $q_field ] );
                } else {
                    delete_post_meta( $post_id, $project_prefix . $q_field ); //delete if not checked
                }
            }
            break;            
    }

}
add_action( 'save_post', 'project_save_quick_edit_featured_data', 10, 2 );

0

Leave a Comment