can't save metabox checkbox

44 Views Asked by At

I m creating a plugin, i listed published pages with checkbox in my metabox. the problem is these checkbox values are not saved to DB.

function display_pages_metabox($post){ 
    $custom = get_post_custom($post->ID);
    $checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );

   wp_nonce_field( 'save_quote_meta', 'custom_nonce' );
   $pages = get_pages();

  foreach ( $pages as $page ) { ?>   
      <input id="page_<?php echo $page->ID; ?>" type="checkbox" name="checkfield[]" value="<?php echo $page->ID; ?>" <?php if ( in_array($page->ID, (array) $checkfield) ) { ?> checked <?php } ?>/>

      <label for="page_<?php echo $page->ID; ?>"><?php echo $page->post_title; ?></label> <br>   
    <?php }
     }
function checkfield(){
    global $post;
    if(isset( $_POST['checkfield[]'] )){   

    $custom = $_POST['checkfield[]'];
    $old_meta = get_post_meta($post->ID, '_checkfield[]', true);                    
        if(!empty($old_meta)){
            update_post_meta($post->ID, '_checkfield[]', $custom);
        } else {
        add_post_meta($post->ID, '_checkfield[]', $custom, true);
        }
 }
}
0

There are 0 best solutions below