I have 7 meta fields for a custom post type in WordPress. These 7 in particular are days of the week (Sunday thru Saturday) and have checkboxes for each. I’ve successfully gotten the checkbox to POST when the value is “On”, however when I return to the edit page and try to deselect, the value stays the same in the database.
Here is what I have for each item input:
<li><input type="checkbox" name="sunday" <?php if( $meta_sunday == true ) { ?>checked="checked"<?php } ?>" /> Sunday<br /></li>
And my update lines:
if(!isset($_POST["sunday"])):
return $post;
endif;
update_post_meta($post->ID, "sunday", $_POST["sunday"] );
It appears the post meta is not being cleared for ‘sunday’ when the checkbox is unchecked.
Or you can set the value to false
Unchecked checkboxes are not set in the $_POST, so you’d have to empty their meta field.
Something like this should work :
Unchecked checkboxes are not set in the $_POST, they are set in $_REQUEST,
you can set checkbox value of custom post type like this:
and get checkbox value like this: