Make WordPress Core

Opened 12 years ago

Last modified 5 years ago

#20853 assigned defect (bug)

get_post_custom show directly serialized data from the post_meta arrays.

Reported by: shadowhck's profile shadowhck Owned by:
Milestone: Priority: normal
Severity: normal Version: 1.5
Component: Options, Meta APIs Keywords: dev-feedback
Focuses: Cc:

Description

get_post_custom show directly serialized data from the post_meta arrays.
For example:

If I save a data with:

			update_post_meta(	$post_id,	'camp',
				array(
					'some'	=>	$var,
					'thing'	=>	$var_2
				)
			);

When I use get_post_meta(); :

	$var_get = get_post_meta($post_id, 'camp', true);
	echo $var_get['some']; // Fine.. Print: $var content.

but, when I use get_post_custom() :

	$var = get_post_custom($post_id);
	echo $var['some']; Metod, (Not work because "Returns a multidimensional array" with all values, and 0 is the first or the unique value).
	echo $var['some'][0]; /* Metod, >:( , Not work. Print:

a:2:{i:some;s:4:"this";i:thing;s:7:"content";}	*/

Instead of display "array", and can not handle sub arrays.

Change History (12)

#1 @shadowhck
12 years ago

  • Owner Alan deleted
  • Status changed from new to assigned

#2 @shadowhck
12 years ago

Edit:

	$var = get_post_custom($post_id);
	echo $var['camp']['some']; Metod, (Not work because "Returns a multidimensional array" with all values, and 0 is the first or the unique value).
	echo $var['camp']['some'][0]; /* Metod, >:( , Not work. Print:

a:2:{i:some;s:4:"this";i:thing;s:7:"content";}	*/

#3 @shadowhck
12 years ago

  • Version set to 3.4

#4 @rosshanney
12 years ago

The Codex page for get_post_custom states:

Note: not only does the function return a multi-dimensional array (ie: always be prepared to deal with an array of arrays, even if expecting array of single values), but it also returns serialized values of any arrays stored as meta values. If you expect that possibly an array may be stored as a metavalue, then be prepared to 'maybe_unserialize'.

#5 @scribu
12 years ago

  • Keywords 2nd-opinion added; needs-patch removed
  • Version changed from 3.4 to 1.5

Yes, that's how it always worked, but I'm not really sure why.

#6 @shadowhck
12 years ago

So, would be good to add an option to this function to deserialize the serialized data?

Example:

get_post_custom($post_id, true);

:/

#7 @scribu
12 years ago

It's not that easy. get_post_custom() is just an alias for get_post_meta(), which is an alias for get_metadata().

#8 @scribu
12 years ago

I mean sure, you could add the deserialization logic directly into get_post_custom() but that would create a big inconsistency.

#9 @shadowhck
12 years ago

  • Severity changed from normal to minor

#10 @pbiron
11 years ago

  • Cc paul@… added

I came across this issue yesterday and was about to open a ticket for it, but then found the documentation in the Codex (and this ticket).

While it is documented, I agree with the original poster that it is a bug. Note that it also applies to get_post_custom_values() and it is not documented there (at least directly, altho there is a reference to the Codex entry for get_post_custom()).

#11 @nacin
10 years ago

  • Component changed from General to Options and Meta

#12 @chriscct7
8 years ago

  • Keywords dev-feedback added; 2nd-opinion removed
  • Severity changed from minor to normal
Note: See TracTickets for help on using tickets.