#42687 closed defect (bug) (fixed)
get_post_meta() is returning arrays even when the $single value is true
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 5.1 | Priority: | normal |
Severity: | normal | Version: | 4.9 |
Component: | Posts, Post Types | Keywords: | needs-docs good-first-bug has-patch |
Focuses: | docs | Cc: |
Description
get_post_meta call:
$post_meta = get_post_meta( $post_id, '', true );
Result:
array(
'post_meta_1' => array('post_meta_1_value'),
'post_meta_2' => array('post_meta_2_value')
)
Expected result
array(
'post_meta_1' => 'post_meta_1_value',
'post_meta_2' => 'post_meta_2_value'
)
Attachments (1)
Change History (9)
#2
follow-up:
↓ 4
@
7 years ago
Hi dd32.
I´m updating meta data as single value and there are no multiple meta_values for any given meta_key.
The error only occurs when i retrieve all post meta data from a post, it´s working well when I specify a meta_key as in your example (second get_post_meta() param)
Thanks!
#3
@
7 years ago
Hi @titodevera welcome to trac!
I have tested same thing in both cases.
First
get_post_meta('ID', 'meta_key', true)
and
get_post_meta('ID', '', true)
. I am getting correct result in both. So I guess as @dd32 mentioned it should be an array value in meta key. May be before you added update_post_meta.
#4
in reply to:
↑ 2
@
7 years ago
- Keywords needs-patch needs-docs good-first-bug added; reporter-feedback removed
- Milestone changed from Awaiting Review to 5.0
Replying to titodevera:
The error only occurs when i retrieve all post meta data from a post, it's working well when I specify a meta_key as in your example (second get_post_meta() param)
My apologies! I missed that.
In that case, It's intentional, and it looks like the documentation needs updating. The function is a wrapper for get_metadata() which does correctly document it.
If someone wants to whip up a patch to update the function docs here that'd be great :)
#5
@
7 years ago
- Keywords has-patch added; needs-patch removed
@dd32
I have created 42687.patch to change documentation for $single in get_post_meta() function. I hope that will be helpful.
Hey @titodevera and welcome to Trac!
Can you check your database directly to see if the metadata for the post is what you expect?
Testing it, it seems to be working as expected for me:
Note, that if you have stored an array in the meta value, it'll return that array as the "single" key - WordPress supports multiple meta_values for any given meta_key.