- This topic has 3 replies, 2 voices, and was last updated 4 months, 3 weeks ago by .
-
Topic
-
Hello!
I created a table like this (relevant columns shown only):
MB_Custom_Table_API::create( "{$wpdb->prefix}_characters", array( 'height_known' => 'BOOLEAN', 'height_imperial' => 'TEXT', 'height_metric' => 'DECIMAL(5, 2) UNSIGNED NOT NULL', ), array() );
And then I created a metabox like this (relevant custom fields only):
$meta_boxes[] = array( 'title' => esc_html__( 'Characters Extra Info', 'my_text_domain' ), 'id' => 'characters-extra-info', 'post_types' => array( CHARACTER_CPT_SLUG ), 'context' => 'normal', 'priority' => 'high', 'autosave' => true, 'storage_type' => 'custom_table', 'table' => "{$wpdb->prefix}_characters", 'fields' => [ [ 'id' => 'height_known', 'type' => 'switch', 'name' => esc_html__( 'Height known?', 'my_text_domain' ), 'std' => 0, 'style' => 'square', 'on_label' => 'Yes', 'off_label' => 'No', 'columns' => 4, 'tab' => 'biological-tab', ], [ 'id' => 'height_imperial', 'type' => 'text_list', 'name' => esc_html__( 'Imperial height', 'my_text_domain' ), 'options' => [ 'ft' => '', 'in' => '', ], 'columns' => 4, 'tab' => 'biological-tab', 'visible' => [ 'when' => [['height_known', '=', 1]], 'relation' => 'and', ], ], [ 'id' => 'height_metric', 'type' => 'text', 'name' => esc_html__( 'Metric height', 'my_text_domain' ), 'placeholder' => esc_html__( 'cm', 'my_text_domain' ), 'columns' => 4, 'readonly' => 1, 'tab' => 'biological-tab', 'visible' => [ 'when' => [['height_known', '=', 1]], 'relation' => 'and', ], ], ], 'tab_style' => 'default', 'tab_wrapper' => true, 'tabs' => array( 'biological-tab' => [ 'label' => 'Biological', 'icon' => 'dashicons-admin-users', ], ),
Here’s the problem: the first time I set the ‘height_imperial’ field, everything is fine. The database column is NULL and it all works out nicely. For values “5 feet 7 inches”, the database column will be:
a:2:{i:0;s:1:"5";i:1;s:1:"7";}
The next time I update the post, regardless of whether I change the values or not, the database changes to:
a:3:{i:0;s:0:"";i:1;s:1:"5";i:2;s:1:"7";}
That extra “0” at the beginning of the array causes the values to shift and the field is not displayed properly anymore, and it’s saved wrongly from now on.
I was able to confirm with the filter ‘rmwb_height_imperial_value’ that the values passed on is correct, namely array(5,7) AND that it all works out perfectly if I disable the custom table storage.
- You must be logged in to reply to this topic.