#17087 closed defect (bug) (invalid)
get_post_meta returns empty array when requesting non-exsistent post meta
Reported by: | thomasvonhassel | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | trivial | Version: | 3.1 |
Component: | General | Keywords: | |
Focuses: | Cc: |
Description
running this
$meta = get_post_meta($post->ID, 'metakey', true);
on a non existent metakey returns an empty array instead of an empty string.
Change History (8)
#2
@
14 years ago
I cant see how that'd be possible looking at the code.
The only case would be, in the event that metakey is null/empty, then the $single param is not applied, as an empty metakey is asking for all metadata; applying single in that case doesn't make sense.
Do you have any metadata filters? Any plugins activated?
#3
@
14 years ago
Just did some more testing.
If i query for a metakey that does not exist at all, for any post, it return an empty string. If i query for a key that exists in some posts, but not the current post, it returns an empty array.
No filters or plugins activated that do anything with the metadata.
#5
@
14 years ago
- Keywords reporter-feedback added
Could you post some code which shows the problem? I'd suggest disabling all plugins and any caching related items as well.
The following code works for me:
<?php update_post_meta(7, 'testkey', array('Value1', 'Value2')); $id = 1; var_Dump( get_post_meta(7, 'testkey', true), get_post_meta(7, 'testkey', false), get_post_meta($id, 'foobar', true), get_post_meta($id, 'foobar', false), get_post_meta($id, 'testkey', true), get_post_meta($id, 'testkey', false) ); ?> Output: array 0 => string 'Value1' (length=6) 1 => string 'Value2' (length=6) array 0 => array 0 => string 'Value1' (length=6) 1 => string 'Value2' (length=6) string '' (length=0) array empty string '' (length=0) array empty
#6
@
14 years ago
and as i said before, an empty key will result in an array regardless (As an empty key means "return all data for this object_id" a singular return in that case seems incorrect)
var_dump( get_post_meta($id, '', true), get_post_meta($id, '', false) ); Output: array empty array empty
#7
@
14 years ago
- Keywords reporter-feedback removed
- Resolution set to invalid
- Severity changed from major to trivial
- Status changed from new to closed
Sorry guys, somehow i have been saving empty arrays into metavalues that should been empty or not set at all ... always check your database first ..
Can't reproduce it.