Title: Issue adding elements to an array in post meta using REST API
Last modified: September 27, 2016

---

# Issue adding elements to an array in post meta using REST API

 *  [nickb](https://wordpress.org/support/users/nickbuc/)
 * (@nickbuc)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/issue-adding-elements-to-an-array-in-post-meta-using-rest-api/)
 * Hi,
 * I have a problem adding elements to an array of user_ids in post meta using the
   REST API. This works well when not integrated with the REST API, but when I do
   it through the API, the new value I add to the array with the list of IDs in 
   post meta always overwrite the previous value, rather than add it in the list.
   I am sure this is something really simple: your help would really GREAT!!!
 * Thanks
 *     ```
       register_rest_field(
           'post',
           'nb_post_registered_user_liked',
           array(
               'get_callback'      => 'nb_get_post_registered_user_liked',
               'update_callback'   => 'nb_update_post_registered_user_liked',
               'delete_callback'   => null,
               'schema'            => $nb_post_registered_user_liked_schema // defined separately
               )
   
       function nb_get_post_registered_user_liked( $object, $field_name, $request ) {
           return get_post_meta( $object['id'], 'nb_post_liked_user_ids', true ); // to test post meta
       }
   
       function nb_update_post_registered_user_liked( $value, $object, $field_name ) {
           $nb_post_liked_user_ids_meta = null;
           $user_key = null;
   
           $user_id = get_current_user_id();
           $nb_post_liked_user_ids_meta = get_post_meta( $object->ID, 'nb2_post_liked_user_ids', true );
           if ( count( $nb_post_liked_user_ids_meta ) != 0 ) {
               $nb_post_liked_user_ids_meta = $nb_post_liked_user_ids_meta[0];
           }
           if ( !is_array( $nbu_post_liked_user_ids ) ) {
       	$nb_post_liked_user_ids_meta = array();
           }
   
           $user_key = array_search( $user_id, $nb_post_liked_user_ids_meta );
           if ( !$user_key ) {
               $nb_post_liked_user_ids_meta['user-' . $user_id] = $user_id;
               $nb_post_liked_user_ids_meta[] = $user_id; // tried also this way...
               update_post_meta( $object->ID, 'nb_post_liked_user_ids', $nb_post_liked_user_ids_meta );
               return nb_post_liked_user_ids_meta; // to test
           };
       }
       ```
   

Viewing 1 replies (of 1 total)

 *  [anor89](https://wordpress.org/support/users/anor89/)
 * (@anor89)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/issue-adding-elements-to-an-array-in-post-meta-using-rest-api/#post-8237750)
 * Programm api shmapi don’t works. Fatal error: Call to undefined function get_rest_url()
   in /home/u605522637/public_html/wp-content/plugins/rest-api/extras.php on line
   30

Viewing 1 replies (of 1 total)

The topic ‘Issue adding elements to an array in post meta using REST API’ is closed
to new replies.

 * ![](https://ps.w.org/rest-api/assets/icon-256x256.png?rev=1346297)
 * [WordPress REST API (Version 2)](https://wordpress.org/plugins/rest-api/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/rest-api/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/rest-api/)
 * [Active Topics](https://wordpress.org/support/plugin/rest-api/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/rest-api/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/rest-api/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [anor89](https://wordpress.org/support/users/anor89/)
 * Last activity: [9 years, 8 months ago](https://wordpress.org/support/topic/issue-adding-elements-to-an-array-in-post-meta-using-rest-api/#post-8237750)
 * Status: not resolved