$_POST['post_type']
недоступен в перехватчике save_post
… Этот перехватчик действий имеет 3 доступных аргумента: $post_id
, $post
и $update
. Поэтому вместо этого используйте:
add_action( 'save_post', 'tcg_save_meta_box_data', 10, 3 );
function tcg_save_meta_box_data( $post_id, $post, $update ) {
// Only for shop order
if ( 'shop_order' != $post->post_type )
return $post_id;
// ...
Лучше: Или используйте непосредственно композитный крюк save_post_shop_order
таким образом:
add_action( 'save_post_shop_order', 'tcg_save_meta_box_data', 10, 3 );
function tcg_save_meta_box_data( $post_id, $post, $update ) {
// Check if our nonce is set (and our custom field)
if ( ! isset( $_POST[ 'tracking_box_nonce' ] ) && isset( $_POST['tracking_box'] ) )
return $post_id;
$nonce = $_POST[ 'tracking_box_nonce' ];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) )
return $post_id;
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// User capability check: Check if the current user can edit an order
if ( ! current_user_can( 'edit_shop_order', $post_id ) )
return $post_id;
// Saving the data
update_post_meta( $post_id, 'Other notes', sanitize_text_field( $_POST[ 'tracking_box' ] ) );
}
Код помещается в файл function.php вашей активной дочерней темы (active theme). Это должно лучше работать.