Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#42687 closed defect (bug) (fixed)

get_post_meta() is returning arrays even when the $single value is true

Reported by: titodevera's profile titodevera Owned by: dd32's profile dd32
Milestone: 5.1 Priority: normal
Severity: normal Version: 4.9
Component: Posts, Post Types Keywords: needs-docs good-first-bug has-patch
Focuses: docs Cc:

Description

get_post_meta call:

$post_meta = get_post_meta( $post_id, '', true );

Result:

array(
'post_meta_1' => array('post_meta_1_value'),
'post_meta_2' => array('post_meta_2_value')
)

Expected result

array(
'post_meta_1' => 'post_meta_1_value',
'post_meta_2' => 'post_meta_2_value'
)

Attachments (1)

42687.patch (830 bytes) - added by chetan200891 7 years ago.
Created patch to change documentation for $single in get_post_meta() function.

Download all attachments as: .zip

Change History (9)

#1 @dd32
7 years ago

  • Keywords reporter-feedback added

Hey @titodevera and welcome to Trac!

Can you check your database directly to see if the metadata for the post is what you expect?

Testing it, it seems to be working as expected for me:

var_dump( get_post_meta( 1789, 'name', true ) );
var_dump( get_post_meta( 1789, 'name', false ) );

results in:

string 'one' (length=3)
array (size=2)
  0 => string 'one' (length=3)
  1 => string 'two' (length=3)

Note, that if you have stored an array in the meta value, it'll return that array as the "single" key - WordPress supports multiple meta_values for any given meta_key.

Last edited 7 years ago by dd32 (previous) (diff)

#2 follow-up: @titodevera
7 years ago

Hi dd32.

I´m updating meta data as single value and there are no multiple meta_values for any given meta_key.

The error only occurs when i retrieve all post meta data from a post, it´s working well when I specify a meta_key as in your example (second get_post_meta() param)

Thanks!

#3 @chetan200891
7 years ago

Hi @titodevera welcome to trac!

I have tested same thing in both cases.

First

get_post_meta('ID', 'meta_key', true)

and

get_post_meta('ID', '', true)

. I am getting correct result in both. So I guess as @dd32 mentioned it should be an array value in meta key. May be before you added update_post_meta.

Last edited 7 years ago by chetan200891 (previous) (diff)

#4 in reply to: ↑ 2 @dd32
7 years ago

  • Keywords needs-patch needs-docs good-first-bug added; reporter-feedback removed
  • Milestone changed from Awaiting Review to 5.0

Replying to titodevera:

The error only occurs when i retrieve all post meta data from a post, it's working well when I specify a meta_key as in your example (second get_post_meta() param)

My apologies! I missed that.

In that case, It's intentional, and it looks like the documentation needs updating. The function is a wrapper for get_metadata() which does correctly document it.

If someone wants to whip up a patch to update the function docs here that'd be great :)

@chetan200891
7 years ago

Created patch to change documentation for $single in get_post_meta() function.

#5 @chetan200891
7 years ago

  • Keywords has-patch added; needs-patch removed

@dd32

I have created 42687.patch to change documentation for $single in get_post_meta() function. I hope that will be helpful.

#6 @chetan200891
7 years ago

  • Focuses docs added

#7 @dd32
7 years ago

  • Owner set to dd32
  • Resolution set to fixed
  • Status changed from new to closed

In 42227:

Docs: Update the documentation for get_post_meta() to clarify that $single has no effect when the $meta_key is not provided.

Props chetan200891, titodevera.
Fixes #42687.

#8 @johnbillion
7 years ago

  • Milestone changed from 5.0 to 5.1
Note: See TracTickets for help on using tickets.