Frontend posting – everything saves other than checkboxes?

I have a form on the frontend of WordPress which is saving all fields except a checkbox array. It displays and saves the correct values if I tick the boxes in the backend post editor, and the ticks show correctly on the front. However ticking a box on the frontend doesnt save! All other fields save correctly from font and back.I have spent a long time trying to figure it out however it doesn’t seem to work, any help would be greatly appreciated. Here’s the code:

$query = new WP_Query( array( 'post_type' => 'property', 'posts_per_page' => '-1' ) ); 

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 

if(isset( $_GET['post'] ) ) {
    if ( $_GET['post'] == $post->ID ) {
        $current_post = $post->ID;
        $options = $custom_meta_fields[7]['options'];
        $checkboxes = get_post_meta($current_post, 'custom_cgtest', true);
}
}
endwhile; endif; 
wp_reset_query();
global $current_post;

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
$post_information = array(
'ID' => $current_post,
'post_title' => ($_POST['post_title']),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post-type' => 'property',
'post_status' => 'publish',
);    

$post_id = wp_update_post($post_information);
if($post_id) { 
update_post_meta($post_id, 'custom_cgtest', ($_POST['cbn']));
}
}

In the page:

Read More
foreach ($options as $option) {  
    echo '<input type="checkbox" name="cbn[]" id="'.$option['value'].'"',$checkboxes && in_array($option['value'], $checkboxes) ? ' checked="checked"' : '',' /> ';
    echo $option['value'];
    }

In functions.php:

array (  
'label' => 'Checkbox Group',  
'desc'  => 'A description for the field.',  
'id'    => $prefix.'cgtest',  
'type'  => 'cgtest',  
'options' => array (  
    'one' => array (  
        'label' => 'Option One',  
        'value' => 'one'  
    ),  
    'two' => array (  
        'label' => 'Option Two',  
        'value' => 'two'  
    ),  
    'three' => array (  
        'label' => 'Option Three',  
        'value' => 'three'  
    )  
)  
)

If you require any more code please let me know.

Related posts

1 comment

  1. Did you check the database for the data?

    My guess is that the problem is here:

    echo '<input type="checkbox" name="cbn[]" id="'.$option['value'].'"',$checkboxes && in_array($option['value'], $checkboxes) ? ' checked="checked"' : '',' /> ';
    

    should be:

    echo '<input type="checkbox" name="cbn[]" id="'.$option['value'].'"'.$checkboxes && in_array($option['value'], $checkboxes) ? ' checked="checked"' : ''.' /> ';
    

    Edit:
    You’re confusing poor PHP. Try enclosing the tertiary function in parenthesis.

    echo '<input type="checkbox" name="cbn[]" id="' . $option['value'] .    '"' . ( $checkboxes && in_array( $option['value'], (array) $checkboxes ) ? ' checked="checked"' : '' ) . ' /> ';
    

Comments are closed.