How to save multi-valued metabox checkbox

2

I have a loop where it shows the taxonomies of the groups in checkbox format, but when saving, it only saves the last%

Would you like to know how to save all checked fields?

<?php
function noticias_dados_meta_box($post){
    $values = get_post_custom( $post->ID );
    $grupos = isset($values['grupos']) ? esc_attr($values['grupos'][0]) : '';
    wp_nonce_field( 'novos_dados_cliente', 'dados_cliente_nonce' );
?> 

    <!-- checkbox GRUPOS -->
    <label for="grupos">Grupos</label> <br> <br>
<?php 
        $categories=get_categories('title_li=&taxonomy=grupo');  
        foreach($categories as $category) { 
            if ($category->term_id == $grupos) {
                echo "<input type='checkbox' name=‘grupos[]' value='$grupos' checked='true' />";
                echo $category->cat_name;
                echo '<br>';   
            } 
            else{ 
                echo "<input type='checkbox' name=‘grupos[]' value='$category->term_id' />"; 
                echo $category->cat_name;
                echo '<br>';    
            }
        }
}// fecha a função noticias_dados_meta_box

add_action( 'save_post', 'noticias_dados_meta_box_save' );
function noticias_dados_meta_box_save( $post_id ){
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;        
    if( !isset( $_POST['dados_cliente_nonce'] ) || !wp_verify_nonce( $_POST['dados_cliente_nonce'], 'novos_dados_cliente' ) ) return;
    if( !current_user_can( 'edit_post' ) ) return;

    if( isset( $_POST['grupos']))
        update_post_meta( $post_id, 'grupos', $_POST['grupos']);
}
?>
    
asked by anonymous 24.02.2014 / 12:52

2 answers

3

To send fields with multiple values it is necessary to add [] to name . So grupos will be an array and then just a foreach to access all the values.

echo "<input type='checkbox' name='grupos[]' value='$grupos' checked='true' />";

The foreach will update every checkbox:

if( isset( $_POST['grupos'])){
   foreach($_POST['grupos'] as $valor_grupo){
      update_post_meta( $post_id, 'grupos', $valor_grupo);
   }
}
    
24.02.2014 / 12:57
2

Change your foreach :

if ($category->term_id == $grupos) {
    echo "<input type='checkbox' name='grupos[]' value='$grupos' checked='true' />";
    echo $category->cat_name;
    echo '<br>';   
} 
else{ 
    echo "<input type='checkbox' name='grupos[]' value='$category->term_id' />"; 
    echo $category->cat_name;
    echo '<br>';    
}

The change made was to add [] in front of name grupos saying that for that same name (groups) there could be more than one value.

Then your response (for this request / POST for example) will be something like this (if you run a var_dump($_POST['grupos']) or print_r($_POST['grupos']) .

Array
(
    [0] => 0
    [1] => 5
    [2] => 13
)

Being 0, 5, 13 the selected values.

From there, just deal with this return and save as desired.

Note: Of course, all of the above is being done as raw / manual as possible, not using any WP helper / function

EDITED

What you can do on the page where you save is the following (or something like this):

$grupos = $_POST['grupos'];

foreach ($grupos as $value) {
  // aqui você mantém o código que executava o save anteriormente
  // PORÉM troca o valor/variavel antigamente adicionada por $value
}

If possible, add your code here.

    
24.02.2014 / 14:13