I’m having multiple check boxes and I want to save the check box and also update it in the update_post_meta so that next time when a user edit the post, he would know which check boxes are already checked. I don’t know where am I getting wrong. Here is my code:
add_action( 'add_meta_boxes', 'add_custom_box' );
function add_custom_box( $post ) {
add_meta_box(
'Meta Box', // ID, should be a string
'Music', // Meta Box Title
'music_meta_box', // Your call back function, this is where your form field will go
'post', // The post type you want this to show up on, can be post, page, or custom post type
'side', // The placement of your meta box, can be normal or side
'core' // The priority in which this will be displayed
);
}
function music_meta_box( $post ) {
// Get post meta value using the key from our save function in the second paramater.
$custom_meta = get_post_meta($post->ID, 'custom-meta-box', true);
?>
<input type="checkbox" name="custom-meta-box[]" value="huge" <?php if(isset($_POST['custom-meta-box']['huge'])) echo 'checked="checked"'; ?> /> Huge <br>
<input type="checkbox" name="custom-meta-box[]" value="house" <?php if(isset($_POST['custom-meta-box']['house'])) echo 'checked="checked"'; ?> /> House <br>
<input type="checkbox" name="custom-meta-box[]" value="techno" <?php if(isset($_POST['custom-meta-box']['techno'])) echo 'checked="checked"'; ?> /> Techno <br>
<?php
}
add_action( 'save_post', 'save_music_meta_box' );
function save_music_meta_box(){
global $post;
// Get our form field
if(isset( $_POST['custom-meta-box'] )) :
$custom = $_POST['custom-meta-box'];
// Update post meta
foreach($ids as $id){
update_post_meta($id, '_custom-meta-box', $custom[$id]);
wp_set_object_terms( $id, $custom[$id], 'Music' );
}
endif;
}
Please Help
Try this logic:-
Hope it help you.