Make WordPress Core

Opened 15 years ago

Closed 15 years ago

#10882 closed enhancement (wontfix)

Add default data to get_*_meta()

Reported by: denis-de-bernardy's profile Denis-de-Bernardy Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.9
Component: General Keywords:
Focuses: Cc:

Change History (10)

#1 @Denis-de-Bernardy
15 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.

#2 @hakre
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 @Denis-de-Bernardy
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 @dd32
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.

#5 @westi
15 years ago

  • Keywords commit removed

#6 @westi
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 @scribu
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;
}

#9 @ryan
15 years ago

  • Milestone changed from 2.9 to 3.0

#10 @Denis-de-Bernardy
15 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.