WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#20752 closed defect (bug) (invalid)

get_post_meta() accepts int as string value only

Reported by: marcella1981 Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: General Keywords:
Focuses: Cc:

Description

With custom fields with keys

custom_field_0
custom_field_1
custom_field_2

Grabbing the data as follows is fine

$field[] = get_post_meta(get_the_ID(), 'custom_field_0');
$field[] = get_post_meta(get_the_ID(), 'custom_field_1');
$field[] = get_post_meta(get_the_ID(), 'custom_field_2');

Return: Field value

However

for($i = 1; $i <= 3; $i++) :
    $field[] = get_post_meta(get_the_ID(), 'custom_field_'.$i);
endforeach;

Return: null

Change History (2)

comment:1 in reply to: ↑ description duck_2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Replying to marcella1981:

for($i = 1; $i <= 3; $i++) :
    $field[] = get_post_meta(get_the_ID(), 'custom_field_'.$i);
endforeach;

This has a syntax error. You're starting a for loop but trying to end it as a foreach loop. That last line should be endfor;.

You're also starting the for loop at 1 and ending with i equal to 3. This means that it's attempting to grab custom_field_1 to custom_field_3 instead of 0 to 2. You want for ($i = 0; $i <= 2; $i++) ...

comment:2 marcella19812 years ago

I am so sorry!

Many apologies, was one of those moments, turned hours, turned loss of sanity.

Note: See TracTickets for help on using tickets.