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 want to change the code below to allow for multiple checkboxes to be selected. It currently saves and updates only one selction. And to update the option again, I have to "uncheck" the previous selection then make another single selection for it to save.

    case 'tax_select': 
$terms = get_terms( $field['id'], 'get=all' ); 
$selected = wp_get_object_terms( $post->ID, $field['id'] ); 
foreach ( $terms as $term ) { 
    printf( '<input type="checkbox" name="%s[]" value="%s" %s/><br />', $field['id'], $term->slug, !empty( $selected ) && !strcmp( $term->slug, $selected[0]->slug ) ? ' checked="checked"' : '' ); 
} 
$taxonomy = get_taxonomy( $field['id'] ); 
echo '<span class="description"><a href="' . get_bloginfo( 'home' ) . '/wp-admin/edit-tags.php?taxonomy=' . $field['id'] . '">Manage ' . $taxonomy->label . '</a></span>'; 
break; 

NEWLY ADDED: HOW I AM SAVING THE DATA:

    // Save the Data
function save_custom_meta($post_id) {
global $custom_meta_fields;

// verify nonce
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
    return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
    if (!current_user_can('edit_page', $post_id))
        return $post_id;
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;
}

// loop through fields and save the data
foreach ($custom_meta_fields as $field) {
            if($field['type'] === 'tax_select') continue;
    $old = get_post_meta($post_id, $field['id'], true);
    $new = $_POST[$field['id']];
    if ($new && $new != $old) {
        update_post_meta($post_id, $field['id'], $new);
    } elseif ('' == $new && $old) {
        delete_post_meta($post_id, $field['id'], $old);
    }
} // end foreach

    // save taxonomies
$post = get_post($post_id);
$category = $_POST['category'];
wp_set_object_terms( $post_id, $category, 'category' );
}
add_action('save_post', 'save_custom_meta');  
share|improve this question
    
i think there is some code missing. how do you save the data? the code above seems to just display the checkboxes. –  Eyal Jun 16 '12 at 14:04
    
Eyal, thank you for taking a look at this. I added the code I'm using to save the data. –  Rob Myrick Jun 16 '12 at 14:42
add comment

1 Answer 1

up vote 0 down vote accepted

After days and days of frustration, I'm proudly answering my own question.

The original code was not displaying multiple checkboxes because I was using the conditional !strcmp( $term->slug, $selected[0]->slug ) to render my "checked" options. Somehow it only returned 1 "checked" value at best.

The correct conditional simply gets the value of the selected option, and then verifies whether or not it exists in the $meta array in order to return a checked="checked" value. Hence the code below:

foreach ( $terms as $term ) {  
echo '<label for="'.$field['id'].'">'.$term->slug.'</label>';

       if (!empty ($selected) && in_array($term->slug, $meta))
echo '<input type="checkbox" name="'.$field['id'].'[]" value="'.$term->slug.'" checked="checked"/><br />';

else echo '<input type="checkbox" name="'.$field['id'].'[]" value="'.$term->slug.'"/><br />';
}  
share|improve this answer
add comment

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.