1

I'm using update_post_meta to save value of an array in the database,

$array_of_something = [['daysOfWeek' => [1],
        'startTime' => '09:00',
        'endTime' => '10:00'], [
        'daysOfWeek' => [2],
        'startTime' => '09:00',
        'endTime' => '10:00'], [
        'daysOfWeek' => [3],
        'startTime' => '09:00',
        'endTime' => '10:00'], [
        'daysOfWeek' => [4],
        'startTime' => '09:00',
        'endTime' => '10:00'], [
        'daysOfWeek' => [1],
        'startTime' => '11:00',
        'endTime' => '12:00'], [
        'daysOfWeek' => [3],
        'startTime' => '11:00',
        'endTime' => '12:00']];
update_post_meta($post_id, 'my_meta_of_the_post', $array_of_something);

based on the documentation of codex:

A passed array will be serialized into a string

but the data is save to database like:

Array Array Array Array Array Array

and when I get the data using

get_post_meta($post_id, 'my_meta_of_the_post', true);

I'll get:

Array Array Array Array Array Array

enter image description here

but I expect that the value get serialize and saved in db, funny thing is if I do exact operation with

$array_of_something = [[['a'=>['value_aa','value_ab'],'b'=>'value_of_b']],[['c'=>['value_ca','value_cb'],'d'=>'value_of_d']]];

the value get serialized and correctly save in the database.

if I serialize the $array_of_something before saving using update_post_meta the value get double serialized in the db i have to use unserialize twice when calling get_post_meta and when I get it back, first unserialized work fine but the second one there is a <p> ... </P> tag around the value, which the value is okay but because of

tag around it it doesn't unserialiezed and return false

1 Answer 1

2

I'm pretty sure there is a local issue somewhere in your code. I ran the code you put in your question and it saved to the database as expected. a:6:{i:0;a:3:{s:10:"daysOfWeek";a:1:{i:0;i:1;}s:9:"startTime";s:5:"09:00";s:7:"endTime";s:5:"10:00";}i:1;a:3:{s:10:"daysOfWeek";a:1:{i:0;i:2;}s:9:"startTime";s:5:"09:00";s:7:"endTime";s:5:"10:00";}i:2;a:3:{s:10:"daysOfWeek";a:1:{i:0;i:3;}s:9:"startTime";s:5:"09:00";s:7:"endTime";s:5:"10:00";}i:3;a:3:{s:10:"daysOfWeek";a:1:{i:0;i:4;}s:9:"startTime";s:5:"09:00";s:7:"endTime";s:5:"10:00";}i:4;a:3:{s:10:"daysOfWeek";a:1:{i:0;i:1;}s:9:"startTime";s:5:"11:00";s:7:"endTime";s:5:"12:00";}i:5;a:3:{s:10:"daysOfWeek";a:1:{i:0;i:3;}s:9:"startTime";s:5:"11:00";s:7:"endTime";s:5:"12:00";}}

Do you have the ability to step debug your code? If possible, I would go into the wp-includes/meta.php file in the WordPress directory and put breakpoints on lines 183, 222 and 267 (as of the latest version of WordPress, but I'm sure the line numbers match back a few versions).

wp-includes/meta.php:183

$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
  • This runs through a WordPress filter (in this case "sanitize_post_meta_my_meta_of_the_post")

wp-includes/meta.php:222

$meta_value = maybe_serialize( $meta_value );
  • This is where WordPress serializes the data, if applicable

wp-includes/meta.php:267

$result = $wpdb->update( $table, $data, $where );
  • This is where WordPress actually store the value in the database

Can you run these and report back to what each values are before and after each line is executed?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I removed whole project setup a new wordpress and added the plugin to new site an it start to work correctly,.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.