#20752 closed defect (bug) (invalid)
get_post_meta() accepts int as string value only
Reported by: |
|
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)
Note: See
TracTickets for help on using
tickets.
Replying to marcella1981:
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++) ...