#37534 closed defect (bug) (invalid)
Saving an array with update_post_meta bug
Reported by: | Pulsar_Media | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.5.3 |
Component: | Options, Meta APIs | Keywords: | |
Focuses: | Cc: |
Description
Hi,
I came across a rather strange bug using update_post_meta. I noticed it after i tried saving a multi-dimensional array.
Basically what happens is my array was getting wrapped inside of a new array each time i saved it to the database using the "update_post_meta" function.
I am guessing this might have something to do with the maybe_serialize function that update_post_meta supposedly uses when saving data?
Anyways, i wanted to bring this to your attention because this bug drove me nuts for several days trying to figure out what the hell was going on.
It was after i manually serialized my array and saved the data that i noticed my serialized string was being returned in an array using get_post_meta.
Anyways, im pretty sure this is some kind of bug...please look into this.
Thanks.
Change History (4)
#2
@
8 years ago
well slap me silly...turns out not passing in the third parameter as true wraps your value in an array...otherwise it doesn't........son of a bitch!
thanks for pointing that out...all good now :)
#3
@
8 years ago
- Keywords reporter-feedback removed
- Milestone Awaiting Review deleted
- Resolution set to invalid
- Status changed from new to closed
FWIW it'll return an array of multiple values in an example such as this:
add_post_meta( 1, 'mykey', 'value1' ); add_post_meta( 1, 'mykey', 'value2' ); $array_of_values = get_post_meta( 1, 'mykey' );
Hi @Pulsar_Media and welcome to Trac
Would you be able to post a piece of code that demonstrates the behaviour you're seeing?
Also, keep in mind that you can have multiple meta_key's of the same name for one post, ensure you're setting the 3rd parameter of
get_post_meta()
totrue
if you only expect a single meta value to exist for that meta key.