Opened 15 years ago
Closed 15 years ago
#10882 closed enhancement (wontfix)
Add default data to get_*_meta()
Reported by: | Denis-de-Bernardy | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 2.9 |
Component: | General | Keywords: | |
Focuses: | Cc: |
Description
Westi seems to insist that #10803 should remain fixed rather than re-opened.
Reasons:
http://core.trac.wordpress.org/ticket/10803#comment:4
http://core.trac.wordpress.org/ticket/10803#comment:10
Patch by azaozz:
http://core.trac.wordpress.org/attachment/ticket/10803/meta-default.patch
Change History (10)
#2
@
15 years ago
IMHO it should return false or WP_Error in case of error. Returning an empty string (or an array containing an empty string [instead of just an empty array()
] is pretty much useless for a data getting function.
Returning a user defined default value is much appreceated by me, I use that principle often with own implementations and it's very useable.
#3
@
15 years ago
agreed... empty string and array are completely useless. in my own apps, I return NULL for this kind of stuff, but the WP default seems to be false or WP_Error. At any rate, this should be changed.
#4
@
15 years ago
- Severity changed from major to normal
- Type changed from defect (bug) to enhancement
IMHO it should return false or WP_Error in case of erro
The output of the function has been defined as such for a long time, thats what its purpose is. Its a Function for theme developers, If they ask for a string, they get a string, if they ask for an array, They get an array, Simple as that.
Can we please keep future comments related to the return of the said functions to the other ticket - #10803? Keep this ticket on topic for adding a default output.
#6
@
15 years ago
- Cc westi added
my 2p
I'm not convince for functionality where in 80+% of cases there is not a default value apart from no value (whether an array or ''
depending on the request) we need to add runtime overhead to every usage of this function by adding defaults.
#7
@
15 years ago
Firstly, just because you can set empty custom fields, doesn't mean you should. When you no longer need a custom field, just delete it. Therefore, returning an empty string or array is ok.
Secondly, like westi said, most of the time you don't have a default value.
If you really need that, just write a wrapper function:
function get_post_meta_with_default($id, $key, $value, $single, $default) { $data = get_post_meta($id, $key, $value, $single); if ( empty($data) ) return $default; return $data; }
Regarding this:
"We don't need this - the function already had clear defined behaviour and it didn't conform to it.
The example code shared above relied on a bug in the code."
the shocking part to me, by the way, is the number of times where I've wondered why in the bloody hell get_post_meta() and its user_meta equivalent weren't returning false as a response in order to highlight that a key did not exist.