Make WordPress Core

Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#17087 closed defect (bug) (invalid)

get_post_meta returns empty array when requesting non-exsistent post meta

Reported by: thomasvonhassel's profile thomasvonhassel Owned by:
Milestone: Priority: normal
Severity: trivial Version: 3.1
Component: General Keywords:
Focuses: Cc:

Description

running this

$meta = get_post_meta($post->ID, 'metakey', true);

on a non existent metakey returns an empty array instead of an empty string.

Change History (8)

#1 @greuben
14 years ago

  • Keywords reporter-feedback added

Can't reproduce it.

$meta = get_post_meta( $post->ID, 'metakey', true ); // returns string(0) ""
$meta = get_post_meta( $post->ID, 'metakey' ); // returns array(0) { }

#2 @dd32
14 years ago

I cant see how that'd be possible looking at the code.

The only case would be, in the event that metakey is null/empty, then the $single param is not applied, as an empty metakey is asking for all metadata; applying single in that case doesn't make sense.

Do you have any metadata filters? Any plugins activated?

#3 @thomasvonhassel
14 years ago

Just did some more testing.

If i query for a metakey that does not exist at all, for any post, it return an empty string. If i query for a key that exists in some posts, but not the current post, it returns an empty array.

No filters or plugins activated that do anything with the metadata.

#4 @thomasvonhassel
14 years ago

  • Keywords reporter-feedback removed

#5 @dd32
14 years ago

  • Keywords reporter-feedback added

Could you post some code which shows the problem? I'd suggest disabling all plugins and any caching related items as well.

The following code works for me:

<?php
update_post_meta(7, 'testkey', array('Value1', 'Value2'));

$id = 1;
var_Dump(
get_post_meta(7, 'testkey', true), 
get_post_meta(7, 'testkey', false),

get_post_meta($id, 'foobar', true),
get_post_meta($id, 'foobar', false),

get_post_meta($id, 'testkey', true), 
get_post_meta($id, 'testkey', false)
);
?>

Output:
 
array
  0 => string 'Value1' (length=6)
  1 => string 'Value2' (length=6)
array
  0 => 
    array
      0 => string 'Value1' (length=6)
      1 => string 'Value2' (length=6)

string '' (length=0)
array
  empty

string '' (length=0)
array
  empty

#6 @dd32
14 years ago

and as i said before, an empty key will result in an array regardless (As an empty key means "return all data for this object_id" a singular return in that case seems incorrect)

var_dump(
get_post_meta($id, '', true), 
get_post_meta($id, '', false)
);

Output:
array
  empty
array
  empty
Last edited 14 years ago by dd32 (previous) (diff)

#7 @thomasvonhassel
14 years ago

  • Keywords reporter-feedback removed
  • Resolution set to invalid
  • Severity changed from major to trivial
  • Status changed from new to closed

Sorry guys, somehow i have been saving empty arrays into metavalues that should been empty or not set at all ... always check your database first ..

#8 @dd32
14 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.