Title: Using update_post_meta inside of a function
Last modified: August 22, 2016

---

# Using update_post_meta inside of a function

 *  [IYBITWC](https://wordpress.org/support/users/iybitwc/)
 * (@iybitwc)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/using-update_post_meta-inside-a-function/)
 * I can’t seem to figure this one out. As a test, i’m using a single post ID, custom
   field key, and a value. Every time I use update_post_meta inside of a function
   it doesn’t work. If I use update_post_meta outside of a function it works fine.
 *     ```
       function article_init(){
       	update_post_meta( 10123, 'phone-number', '8885551234');
       }
       ```
   
 * My ultimate goal is to populate those three args using variables instead of constants.
   Which is the reason I’m trying to use this inside of a function. Any idea why
   this isn’t working for me?
 * Thanks,
    IYBITWC

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

 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [11 years, 6 months ago](https://wordpress.org/support/topic/using-update_post_meta-inside-a-function/#post-5541739)
 * How and where are you calling the article_init() function?
 *  Thread Starter [IYBITWC](https://wordpress.org/support/users/iybitwc/)
 * (@iybitwc)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/using-update_post_meta-inside-a-function/#post-5541746)
 * I have a class that has the function in it. Here is a shortened example.
 * **Setup:**
 *     ```
       class PostArticle {
   
       	public function article_init(){
       		update_post_meta( 10123, 'phone-number', '8885551234');
       	}
   
       }
       ```
   
 * **Use:**
 *     ```
       $Article = new PostArticle;
       $Article->article_init();
       ```
   
 *  Thread Starter [IYBITWC](https://wordpress.org/support/users/iybitwc/)
 * (@iybitwc)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/using-update_post_meta-inside-a-function/#post-5541891)
 * Seems I didn’t ask the right question, the code I provided appears to work fine.
 * Update:
    [https://wordpress.org/support/topic/adding-custom-fields-to-wordpress_postcontroller?replies=1#post-6298145](https://wordpress.org/support/topic/adding-custom-fields-to-wordpress_postcontroller?replies=1#post-6298145)

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

The topic ‘Using update_post_meta inside of a function’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 2 participants
 * Last reply from: [IYBITWC](https://wordpress.org/support/users/iybitwc/)
 * Last activity: [11 years, 6 months ago](https://wordpress.org/support/topic/using-update_post_meta-inside-a-function/#post-5541891)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
