我正在为我的公用工程公司开发一个工作跟踪数据库,在运行save挂钩时遇到了一个问题。我有一个task\\u types db表,任务是在该作业的“cover\\u页”(后期编辑屏幕)上使用自定义元框创建的,我允许通过用户输入为每个作业创建这些任务。它正在处理我的验证代码两次,因此从其任务类型创建了两个数据库条目和两个相同的任务,这是不正确的。有人能帮我解决这个问题吗。请参见下面的代码片段。
// 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\');