Title: get_post_meta, get_post_custom array indexing problems
Last modified: August 19, 2016

---

# get_post_meta, get_post_custom array indexing problems

 *  [berny10dos](https://wordpress.org/support/users/berny10dos/)
 * (@berny10dos)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/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 2
 * **The order I entered them in the admin area is ascending:**
 * Title 1
    Title 2 Title 3 Title 4
 * Does anyone know what’s happening?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [paperpariah](https://wordpress.org/support/users/paperpariah/)
 * (@paperpariah)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/get_post_meta-get_post_custom-array-indexing-problems/#post-1164846)
 * I have the same problem, meta data seems to be ordered by date modified not meta_id
   like you would expect 🙁
 *  [TinkerWorkshop](https://wordpress.org/support/users/tinkerworkshop/)
 * (@tinkerworkshop)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/get_post_meta-get_post_custom-array-indexing-problems/#post-1164855)
 * 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/](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).'-->';
       }
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘get_post_meta, get_post_custom array indexing problems’ is closed to 
new replies.

## Tags

 * [get_post_custom](https://wordpress.org/support/topic-tag/get_post_custom/)
 * [get_post_meta](https://wordpress.org/support/topic-tag/get_post_meta/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 3 participants
 * Last reply from: [TinkerWorkshop](https://wordpress.org/support/users/tinkerworkshop/)
 * Last activity: [15 years, 10 months ago](https://wordpress.org/support/topic/get_post_meta-get_post_custom-array-indexing-problems/#post-1164855)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
