get_post_meta, get_post_custom array indexing problems
-
Hi,
I’m experiencing some very weird array indexing problems for get_post_meta and get_post_custom.
In short, I want to have a post where I can enter multiple custom fields of the same type(name)and then extract the value of each and display them on a page in the order they were entered (in my case, the homepage).
I should also mention I am doing this outside of the Loop.
Instead of receiving an array that contains the custom field values in the same order I entered them in the admin area, the entries are mixed.
I tried this:
<?php $custom_fields = get_post_custom(244); $my_custom_field = $custom_fields['field_title_here']; foreach ( $my_custom_field as $key => $value ) echo $key . " => " . $value . "<br />"; ?>and this
<?php $my_custom_field2 = get_post_meta(244,'field_title_here',false); foreach ( $my_custom_field2 as $key => $value ) echo $key . " => " . $value . "<br />"; ?>Example output:
0 => Title 3
1 => Title 4
2 => Title 1
3 => Title 2The order I entered them in the admin area is ascending:
Title 1
Title 2
Title 3
Title 4Does anyone know what’s happening?
-
I have the same problem, meta data seems to be ordered by date modified not meta_id like you would expect 🙁
Had the same problem.
Ended up putting together a function to select custom fields.General notes here:
http://blog.tinkerworkshop.com/wordpress-get_post_custom-wrong-order/Function is:
function get_post_custom_X($id){ global $wpdb; $querystr = "SELECT wposts.ID, wpostmeta.meta_ID, wpostmeta.meta_key, wpostmeta.meta_value ". "FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta ". "WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = ".$id." ". "ORDER BY wpostmeta.meta_id ASC"; $pageposts = $wpdb->get_results($querystr, OBJECT); $return = array(); foreach($pageposts AS $key=>$val){ $return[$val->meta_key][] = $val->meta_value; } return $return; //print '<!--'.print_r($return).'-->'; }
The topic ‘get_post_meta, get_post_custom array indexing problems’ is closed to new replies.