Title: get_post_meta Returns an Empty Array inside a Function
Last modified: August 31, 2016

---

# get_post_meta Returns an Empty Array inside a Function

 *  Resolved [Ryan Stutzman](https://wordpress.org/support/users/ryan-stutzman/)
 * (@ryan-stutzman)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/get_post_meta-returns-an-empty-array-inside-a-function/)
 * In a function with a form that grabs a string, the following code prints an array
   containing the comma-separated data in that string:
 *     ```
       $locations_string = esc_html( get_post_meta( $item_id, 'locations', true ) );
       $locations_array = explode(',', $locations_string);
       $locations_array_trimmed = array_map('trim', $locations_array);
       Print_r($locations_array_trimmed);
       ```
   
 * and the variable ‘locations’ is saved to the database.
 * However, the code prints an empty array in a function further down the page. 
   Any ideas why this might be?

Viewing 1 replies (of 1 total)

 *  Thread Starter [Ryan Stutzman](https://wordpress.org/support/users/ryan-stutzman/)
 * (@ryan-stutzman)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/get_post_meta-returns-an-empty-array-inside-a-function/#post-7049548)
 * Turns out I was declaring my variables out of scope.

Viewing 1 replies (of 1 total)

The topic ‘get_post_meta Returns an Empty Array inside a Function’ is closed to 
new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Ryan Stutzman](https://wordpress.org/support/users/ryan-stutzman/)
 * Last activity: [10 years, 4 months ago](https://wordpress.org/support/topic/get_post_meta-returns-an-empty-array-inside-a-function/#post-7049548)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
