Support » Themes and Templates » Saving checkboxes in custom post type

  • Hi

    I’m working extensively with custom post types. Although I can get it to save any single value – I am having issues figuring out how to save multiple answers – ie checkboxes.

    This is roughly the code I use:

    I declare the checkboxes like this:

    "languages" => array(
    "name" => "languages",
    "title" => "Languages",
    "description" => "",
    "style" => "checkboxes",
    "options" => array(
    "1" => "option1",
    "2" => "option2",
    "3" => "option3",
    "4" => "option4")

    I display it like so:

    elseif(($meta_box['style'] == 'checkboxes') && (!empty($meta_box['options']))) {
    echo '<td>';
    foreach($meta_box['options'] as $radio_value) {
    echo '<input type="checkbox" name="'.$meta_box['name'].'_value'.'" value="'.$radio_value.'"';
    if ($meta_box_value == $radio_value) { echo ' checked="yes"'; }
    echo '/> '.$radio_value. '<br/>';
    echo '</td>';

    and my save function – which works for eveything except obviously checkboxes:

    if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
    add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
    elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
    update_post_meta($post_id, $meta_box['name'].'_value', $data);
    elseif($data == "")
    delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));

    How can I adjust this to save checkboxes – of course the question would also be whether it would require a db row for each option or a single row and if so, I would probably need to explode it first.

    any ideas would be greatly appreciated.

    Thanks in advance

  • The topic ‘Saving checkboxes in custom post type’ is closed to new replies.