Make WordPress Core

Opened 5 years ago

Closed 4 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:

Change History (10)

comment:1 Denis-de-Bernardy5 years ago

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.

comment:2 hakre5 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.

comment:3 Denis-de-Bernardy5 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.

comment:4 dd325 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.

comment:5 westi5 years ago

  • Keywords commit removed

comment:6 westi5 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.

comment:7 scribu5 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;

comment:9 ryan4 years ago

  • Milestone changed from 2.9 to 3.0

comment:10 Denis-de-Bernardy4 years ago

  • Milestone 3.0 deleted
  • Resolution set to wontfix
  • Status changed from new to closed

sounds more like a wontfix, to me...

Note: See TracTickets for help on using tickets.