save post running twice and creating two database entries – how do I disable thi
-
I am developing a job tracking database for my utility engineering company and am having an issue when running a save hook. I have a task_types db table that tasks are created off of and using a custom meta box on the “cover_page” (post edit screen) of the job I allow these tasks to be created for each job from user input. It is processing my validation code twice therefor creating 2 database entries and two identical tasks from its task type, which is not correct. Can anyone assist me with this problem. See the code snippets below.
// Validate Data for Custom Meta Boxes function rfd_argument_function() { global $post; global $rfd_meta_box_options; foreach($rfd_meta_box_options as $rfd_meta_box_option){ $meta_box_options[] = $rfd_meta_box_option['meta_box_form_fields']; } foreach($meta_box_options as $options){ foreach ($options as $option){ if($option['name']){ $arguments = array( 'post_id' => $post->ID, 'name' => $option['name'], ); } do_action('save_hook', $arguments); } } } add_action( 'save_post', 'rfd_argument_function'); // Save data for Custom Meta Boxes function rfd_save_meta_box_values( $args ) { $post_id = $args['post_id']; $name = $args['name']; $old_data = get_post_meta($post_id, $name); $new_data = $_POST[$name]; // Autosave, do nothing if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // Check permissions if ( 'page' === $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id ) ) { return; } }else { if ( !current_user_can( 'edit_post', $post_id ) ) { return; } } if( $_POST['job-update-action']==='freeze'){ // Freeze associated items updated below in save_rfd_job_update_action. return; } // AJAX? Not used here if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return; } // Return if it's a post revision if ( false !== wp_is_post_revision( $post_id ) ){ return; } // if new is same as old if($new_data === $old_data[0] ){ return; }else{ // add_post_meta($post_id, 'save-count', 0, true); // add "save-count" if none exists // $save_count = get_post_meta($post_id, 'save-count', true); // grab save count // if( $save_count == 0){ // $save_count++; // update_post_meta( $post_id, 'save-count', $save_count ); if($_POST['task-to-assign'] !== 'Select a Task Type to Assign'){ global $wpdb; $table_name = $wpdb->prefix . 'task_types'; $task_type = $wpdb->get_results("SELECT * FROM $table_name WHERE task_name = '" . $_POST['task-to-assign'] . "'", OBJECT); $table_name = $wpdb->prefix . 'tasks'; // Change table // check for duplicate task $task = $wpdb->get_results("SELECT * FROM $table_name WHERE task_name = '" . $_POST['task-to-assign'] . "' AND job_id = '" . $post_id . "'", OBJECT); // rfd_debugger( $task[0],1); // if( $task[0]->job_id === false){ $rows_affected = $wpdb->insert( $table_name, array( 'task_type_id' => $task_type[0]->id, 'task_name' => $task_type[0]->task_name, 'description' => $task_type[0]->description, 'job_id' => $post_id, 'assigned' => $_POST['person-to-assign'], 'assigned_datetime' => current_time('mysql'), ) ); // } } // }else{ // delete_post_meta($post_id, 'save-count', 0); // } update_post_meta($post_id, $name, $new_data); // Add New Data update_post_meta($post_id, 'task-to-assign', 'Select a Task Type to Assign'); // Reset Field update_post_meta($post_id, 'person-to-assign', 'Select A Person'); // Reset Field } } add_action( 'save_hook', 'rfd_save_meta_box_values');
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘save post running twice and creating two database entries – how do I disable thi’ is closed to new replies.