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 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');
share|improve this question

2 Answers 2

Try moving your

 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  return;
 }

into your rdf_argument_function(). Any change?

share|improve this answer
    
nah but thanks... It is caused because in the functions before this that I have created to make my meta box creation more streamlined, I am calling this function many times. I worked around this by just adding in another function and added a save_post call separate from this. Sometimes I make myself feel like an jackass and this is one of those times. lol anyhow thanks again. –  Robert Ruby II Jun 18 '13 at 21:09
up vote 0 down vote accepted

Mark this as RESOLVED OR DELETE. It was caused by a function that I myself created to help with meta box creation.

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.