WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#17087 closed defect (bug) (invalid)

get_post_meta returns empty array when requesting non-exsistent post meta

Reported by: 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)

comment:1 greuben3 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) { }

comment:2 dd323 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?

comment:3 thomasvonhassel3 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.

comment:4 thomasvonhassel3 years ago

  • Keywords reporter-feedback removed

comment:5 dd323 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

comment:6 dd323 years ago

and as i said before, an empty key will result in an array regardless

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

Output:
array
  empty
array
  empty
Version 0, edited 3 years ago by dd32 (next)

comment:7 thomasvonhassel3 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 ..

comment:8 dd323 years ago

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