2
I have a loop where it shows the taxonomies of the groups in the format of checkbox
, but when he saves, he saves only the last checkbox
marked.
I would like to know how to save all fields checked?
<?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']);
}
?>
Hello 'lost'! The foreach you are talking about is this: foreach($Categories as $Category)? Because when I add [ ] no value is saved.
– Flávia Amaral
No, the
foreach
is to retrieve checkboxes marked.– rray
I still could not do. I am not good in php, I am starting now. I do not know where to insert this other foreach to recover the data.
– Flávia Amaral
This foreach you put in the file that in the tag action
<form>
.– rray
I don’t have the <form> tag because I’m using Metabox to show the checkbox.
– Flávia Amaral