Unserialize Multicheck
-
I have been using the following code to unserialize a Multicheck type field when the field is updated in the backend. The code used to work.. but doesn’t anymore. I looked at the database and the fields are still serialized.
Code that populates the array for the “options” field of Multiclient
$multiClient = array(); $client_query = new WP_Query(); $client_query->query(array('post_type' =>'cad_clients','orderby' => 'title','order' => 'ASC','posts_per_page' => -1)); if ($client_query->have_posts()) : while ($client_query->have_posts()) : $client_query->the_post(); global $post; $multiClient[get_the_ID()] = get_the_title( get_the_ID() ); endwhile;endif;
Code for the meta box for the field:
$meta_boxes[] = array( 'id' => 'film_metabox_side', 'title' => 'Filmography Fields', 'object_types' => array( 'cad_films', ), // Post type 'context' => 'side', 'priority' => 'high', 'show_names' => true, // Show field names on the left 'fields' => array( array( 'name' => 'Client Names', 'id' => $prefix . 'film_client_multi_name', 'type' => 'multicheck', 'options' => $multiClient, ), ), );
My function to Unserialize:
function test_select_multiple_save($object_id, $meta_box_id, $updated, $meta_box) { // check that we are updating the right metabox data and the field has been changed/updated if( $meta_box_id == 'film_metabox_side' && in_array('_cmb_film_client_multi_name',$updated) ) : delete_post_meta($object_id, '_cmb_film_client_multi_name'); // first delete all meta foreach($_POST['_cmb_film_client_multi_name'] as $value) { // add new meta data as multiple meta fields add_post_meta($object_id, '_cmb_film_client_multi_name', $value); } endif; } add_action( 'cmb2_save_post_fields', 'test_select_multiple_save', 99, 4);
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Unserialize Multicheck’ is closed to new replies.