Leave a Reply

3 comments

  1. 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;
    
  2. 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 );
    
  3. Unchecked checkboxes are not set in the $_POST, they are set in $_REQUEST,
    you can set checkbox value of custom post type like this:

    if ( isset( $_REQUEST['inprint'] ) ) {
       update_post_meta($post_id, '_inprint', TRUE);
     } else {
       update_post_meta($post_id, '_inprint', FALSE);
     }
    

    and get checkbox value like this:

    global $post;
    
    $inprint= get_post_meta($post->ID, '_inprint', true);
    
    <input type="checkbox" name="inprint" value='1'<?php checked(1, $inprint); ?> />