0

Below is how I serialize my array.

$featured_image_id = $arg['thumbnail_id'];
$data = serialize( array( $featured_image_id ) );

update_post_meta( $arg['post_id'], 'images', $data );

What I get in my db table is s:17:"a:1:{i:0;i:1955;}"; with a string in front.

How do I remove the string? I just need "a:1:{i:0;i:1955;}"

codanad376
  • 65
  • 6
  • 2
    Why are you serializing this at all? Why not just store the `$featured_image_id` in your table? – Tangentially Perpendicular Mar 25 '22 at 02:25
  • @TangentiallyPerpendicular because one of the plugins requires the meta_value to be serialized. – codanad376 Mar 25 '22 at 02:28
  • I saw this https://stackoverflow.com/questions/64721616/correct-way-to-insert-serialized-data-into-wordpress-post-meta-using-update-post but I still don't get the answer `$data = unserialize( 'YOUR_CODE_SERIALIZED_HERE' );` does it say it wants me to save the data as unserialized? – codanad376 Mar 25 '22 at 02:29

1 Answers1

0

Ok, I fixed it.

I don't need to serialize the array. It will be serialized by update_post_meta function as according to https://developer.wordpress.org/reference/functions/update_post_meta/

WordPress doc is always not clear. It should change from saying "Metadata value. Must be serializable if non-scalar." to "Metadata value. Value will be automatically serialized if non-scalar." PHP doc is still the best.

codanad376
  • 65
  • 6