Metabox 数据不更新

Metabox data does not update

我有一个自定义的 post 类型,它有一个带有输入字段的元数据框 现在我添加了负责保存值的代码。 在此之后我查看添加数据库并且未保存值。

<div class="fieldGroup">
    <label class="labelBlcnr">Prijs</label>
    <span class="currency">&euro;</span>
    <input type="text" class="price" name="item_price" id="item_price" value="<?php echo $price; ?>"/>
</div>

<div class="fieldGroup">
    <label class="labelBlcnr">Intern nummer</label>
    <input type="text" name="intern_sku"/>
</div>


<?php
add_action( 'save_post', function() {
    global $post;
    if ( array_key_exists( 'item_price', $_POST ) ) {
        update_post_meta( $post->ID, '_item_price', $_POST['item_price'] );
    }   
});
?>

如果我回显 $post->ID 我得到了正确的 ID。

希望有人能帮助我

最好使用 do_action( "save_post_{$post->post_type}", int $post_ID, WP_Post $post, bool $update ) 检查下面的代码。

add_action( 'save_post_your_custom_post_type', 'update_meta_box_value', 10, 3 );
function update_meta_box_value( $post_id, $post, $update ){
    if( isset( $_POST['item_price'] ) && $_POST['item_price'] != '' ) {
        update_post_meta( $post_id, '_item_price', $_POST['item_price'] );
    }
}