Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Adding and updating repeating custom field meta data

Since you’re using the same key for all the values you can simply use:

update_post_meta($this->current_post_id, 'assigned-sales', $response->records);

This will store the whole array in a single meta entry with key of ‘assigned-sales’. WordPress will update the value if it exists and create new one otherwise, no need to have logic for that.

In case you insist on having separate meta key for each array entry you should add some sort of index to each meta key to make it unique (this is assuming you want each one and not just the latest value of the same).

I think this should do the trick:

foreach ($response->records as $key=>$record) {
  $sales="ten"; // <-- Where is this coming from?
  update_post_meta($this->current_post_id, 'assigned-sales-'.$key, $sales);
}

And here also the logic for add/update is redundant and you don’t need a separate function.

It all depends on what you’re after so if your issue persist, please clarify the question and what the use of the data is.

Related Posts:

  1. Add options to featured image
  2. Assign category using custom field?
  3. What WordPress function to use to get meta value by using meta keys?
  4. Default Custom Field Value Automatically Update
  5. Prevent custom field from being updated after first publish
  6. How to change text color depending on the number value (Using javascript)
  7. Reference multiple style sheets, clearing styles for permalink page, custom fields for css
  8. Creating a “Related Meta” type field?
  9. update_post_meta not working in function
  10. Add custom fields after post/page title
  11. How to display custom field in woocommerce orders in admin panel?
  12. Programatically add options to “add new” custom field dropdown
  13. Get a list of all available fields in the User Profile
  14. Enabling shortcodes for custom fields
  15. Detect meta value changes when post is updated (post_updated)
  16. Search everything (posts, pages, tags, cpt, meta)
  17. Change meta tags programatically
  18. Authors details such as social media links, emails etc → Is this Meta or something else?
  19. Use a function to update post meta based on other post meta
  20. post meta data clearing on autosave
  21. update_post_meta for custom field not working upon form submission
  22. How can update custom meta for all posts
  23. Function to Download External Images to My Site
  24. Checked() showing up twice in custom fields
  25. Convert User ID’s to User Names in a single.php file
  26. How to use max and min values of custom fields
  27. Woocommerce Storefront WordPress Ignore Media Gallery Images and use External 3rd Party Host
  28. Customizer field value into functions.php variable
  29. Can’t check if a post has thumbnail adding filter to get_post_metadata()
  30. Auto delete post if certain custom field data is empty
  31. Move Post to different category if post_meta field is 0 or is 2 days old?
  32. How to display custom option field in woocommerce orders in admin panel?
  33. How to add a slide toggle to the Woocommerce-Layout__Activity-Panel area in the orders admin page
  34. get_post_meta not working on publishing
  35. Removing custom meta data
  36. Change label for registration Fields
  37. Custom Attachment Caption Fields
  38. Post meta not updating
  39. Function to update post_meta based on existing post_meta
  40. WordPress function saves a post twice and updates all posts
  41. ACF not showing
  42. Exclude from the query posts with meta_key and meta_value
  43. How to retrieve the values of a sub-field in the first and last row of an (ACF) repeater inside function?
  44. How to get correct value from checked()?
  45. Get array of metakey in all posts
  46. Custom Behavior when Adding New Custom Post Type in Dashboard
  47. User function to return multiple get_post_meta()
  48. Hide custom fields when empty
  49. What are the meta fields for an attachment?
  50. Programmatically add Yoast meta description after post save
  51. Insert Address fields into function
  52. How to make custom comment fields required
  53. How add body_class from wp_termmeta
  54. How do I display a date correctly that is stored in the database as a backwards 8 digit number from Advanced Custom Fields? [closed]
  55. Sort and display pages with specific custom field (not tag)
  56. shorthand syntax for custom fields
  57. Fill empty custom field with other custom field
  58. Hide a div when a custom field is empty
  59. saving/reading custom field value does not work – no value gets POSTed
  60. Perform function on publish AND save (not just save)
  61. How to avoid saving empty data to sql while using add_meta_box
  62. How to Display WooCommerce Product Price in WooCommerce Archive
  63. How to add custom metakey to shop_order page’s searching function?
  64. wp_set_object_term via js btn frontend
  65. Populate Product Regular Price with a calculated ACF Field Value
  66. Update wp_postmeta table based on 2 keys
  67. blank page with wp_get_attachment
  68. How Can I Concatenate A String With One Of My Custom Field Value Before Saving The Post?
  69. Create New User Custom Field not Saving
  70. How To Display Author Popup on Entry Meta (Genesis Framework)?
  71. How would I correctly implement a new if statement in a child functions file?
  72. Hide empty custom field
  73. Remove post from latest posts after a month only with certain tag
  74. save_post doesn’t correctly process function call with php class
  75. How To Show Shortcodes In WordPress Custom Fields?
  76. Return ACF Field value function
  77. How to echo the translated custom field?
  78. How to add function to custom fields when page/post has a password?
  79. How to exclude posts with empty and non-existent relational custom field with meta_query?
  80. Post date on Custom field
  81. Dynamically generated Navigation Diagram using Custom Fields
  82. Define an extra field for all pages (with no plugin?)
  83. Adding custom field in menu options [duplicate]
  84. How To Get WordPress Categories Last Update Date?
  85. How to add a meta information to the URL?
  86. Automatically add custom fields (post_meta) to all published posts, hourly, via wp_cron?
  87. How to display data from custom fields in my custom shortcode?
  88. Syling Custom Fields echo’s from from functions.php
  89. update_post_meta after form is submited
  90. Display post_meta-by_key on product catalogue
  91. How to save custom made object in an array in a post meta field
  92. Change Post status based on custom field date +1 day
  93. Shortcode parse error – wrong syntax
  94. How to get specific post meta by title or id
  95. Get custom fields without _edit_last, _edit_lock, _wp_page_template and _visual-subtitle
  96. Getting out side of wp root folder from function.php using absolute path
  97. Strange error from functions.php files (wp_register_script)
  98. Create a pdf from the entries in DB
  99. Enqueue JS + CSS via a child theme functions.php file?
  100. How to display milliseconds instead of seconds using timer_stop function? [closed]
Categories functions Tags custom-field, functions, post-meta
tax_query not working properly with get_posts
Display (custom) post number of views

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress