unction save_profi_meta_data($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
if (!isset($_POST['post_type'])) { return $post_id;}
// Создаем массив данных для обновления
$order_num = sanitize_text_field($_POST['order_num']);
$order_title = sanitize_text_field($_POST['order_title']);
// Создаем новый заголовок
$new_title = "Заказ № {$order_num} - {$order_title}";
$new_permalink = "order_{$order_num}";
$post_data = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_content' => sanitize_textarea_field($_POST['order_description']),
'post_name' => $new_permalink,
);
// Добавляем произвольные поля в массив данных
$post_data['meta_input'] = array(
'order_title' => sanitize_text_field($_POST['order_title']),
'order_num' => sanitize_text_field($_POST['order_num']),
'profi_link' => sanitize_text_field($_POST['profi_link']),
'incoming_date' => sanitize_text_field($_POST['incoming_date']),
'deadline' => sanitize_text_field($_POST['deadline']),
'order_description' => sanitize_textarea_field($_POST['order_description']),
'order_sources' => sanitize_text_field($_POST['order_sources']),
'client_name' => sanitize_textarea_field($_POST['client_name']),
'client_contacts' => sanitize_textarea_field($_POST['client_contacts']),
'order_status' => sanitize_text_field($_POST['order_status']),
'current_task' => sanitize_textarea_field($_POST['current_task']),
'order_price' => sanitize_text_field($_POST['order_price']),
'profi_commission' => sanitize_text_field($_POST['profi_commission']),
'self_employed' => isset($_POST['self_employed']) && $_POST['self_employed'] === 'on' ? true : false,
);
wp_update_post($post_data);
function save_profi_meta_data($post_id) {
// Проверяем, что это не автосохранение и что тип записи - "profi"
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
if ('profi' !== get_post_type($post_id)) return $post_id;
// Массив полей для сохранения
$fields_to_save = array(
'order_title',
'order_num',
'profi_link',
'incoming_date',
'deadline',
'order_description',
'order_sources',
'client_name',
'client_contacts',
'order_status',
'current_task',
'order_price',
'profi_commission',
);
$meta_input = array();
foreach ($fields_to_save as $field) {
if (isset($_POST[$field])) {
$meta_input[$field] = sanitize_text_field($_POST[$field]);
}
}
// Обновляем заголовок и ссылку на запись
$order_num = isset($_POST['order_num']) ? sanitize_text_field($_POST['order_num']) : '';
$order_title = isset($_POST['order_title']) ? sanitize_text_field($_POST['order_title']) : '';
$new_title = "Заказ № {$order_num} - {$order_title}";
$new_permalink = "order_{$order_num}";
// Обновляем данные записи
$updated_post_data = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_content' => sanitize_textarea_field($_POST['order_description']),
'post_name' => $new_permalink,
);
// Добавляем произвольные поля в массив данных
$updated_post_data['meta_input'] = $meta_input;
// Обновляем запись
wp_update_post($updated_post_data);
}
// Вызываем функцию сохранения мета-полей при сохранении поста
add_action('save_post', 'save_profi_meta_data');