I hava a custom post type with an own metabox with input fields Now I add the code what is responsible for saving the values. After this I look add the database and the values are not saved.
<div class="fieldGroup">
<label class="labelBlcnr">Prijs</label>
<span class="currency">€</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'] );
}
});
?>
If I echo $post->ID I got the right ID.
Hope somebody can help me
Better to use do_action( "save_post_{$post->post_type}", int $post_ID, WP_Post $post, bool $update ) check below code.
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'] );
}
}