$meta = get_post_meta($post_id,'',true);
brings back all the meta values for $post_id but it returns an array for each value, as shown below:
I might expect this if I had set the third parameter – single – to false but it’s set to true. I haven’t found anything in the codex that talks about what exactly is returned when key is blank.
Does someone have to information here and know how I can get back all the keys with each key value being a single value instead of an array of values?
The answer is: This is by design, but does not appear to be documented in the Codex.
If you look at the true documentation (the source code), you will see that
get_post_meta
callsget_metadata
. By inspecting the code ofget_metadata
, we can see that if the$meta_key
is not posted, then it returns the value before it evaluates if$single
is set or not:If you are using PHP 5.3+, you can get what you want with something like this:
Or, if you want to get really fancy, you can write your own “wrapper” around the get_post_meta function, like so:
Which you could then use like so:
Or, if you aren’t on PHP 5.3+ (you should be!), you could do it like so:
I think, there is no way to get single value in get_post_meta when $single is true. So you write a custom function to get it.
Use