Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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"] );
share|improve this question
    
If I'm not mistaken, an unchecked checkbox is not set in $_POST. You'd have to empty the meta for the checkboxes that are not set. –  Shane Feb 7 '12 at 17:00

2 Answers 2

up vote 3 down vote accepted

It appears the post meta is not being cleared for 'sunday' when the checkbox is unchecked.

If( isset($_POST['sunday']) ){
    update_post_meta($post->ID, "sunday", $_POST["sunday"] );
}else{
    delete_post_meta($post->ID, "sunday");
}
return $post;

Or you can set the value to false

If( isset($_POST['sunday']) ){
    update_post_meta($post->ID, "sunday", true );
}else{
    update_post_meta($post->ID, "sunday", false );
}
return $post;
share|improve this answer
    
Just consolidating your code a little, update_post_meta($post->ID, "sunday", isset($_POST['sunday']) ); –  Matthew Boynes Feb 7 '12 at 22:14
    
Thanks @MatthewBoynes and Ryan Meier - this worked perfect –  javy Feb 8 '12 at 14:37

Unchecked checkboxes are not set in the $_POST, so you'd have to empty their meta field.

Something like this should work :

 $sunday = ( isset( $_POST['sunday'] ) ) ? $_POST['sunday'] : "";
 update_post_meta( $post->ID, 'sunday', $sunday );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.