Title: Add fields with loop
Last modified: October 3, 2020

---

# Add fields with loop

 *  Resolved [pstidsen](https://wordpress.org/support/users/pstidsen/)
 * (@pstidsen)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/add-fields-with-loop/)
 * Hi there,
 * I want to insert a different number of fields on each post. Therefore I use a
   foreach loop to generate the fields. And it actually works – the fields are outputted
   on the edit screen.
 * However after adding images in the fields and saving the post, the images disappear.
   I have searched for the field in the database and there is nothing. Why is the
   data not saved?
 * My loop looks just like this:
 *     ```
       foreach($colors as $color){
         $meta_boxes[] = [
       	'title'      => "Add images of " . $color->name,
       	'id'         => 'id_'.$post_id . '_color_' . $color->slug,
       	'post_types' => ['product'],
       	'context'    => 'normal',
       	'priority'   => 'high',
       	'fields'     => [
       		[
       		'type'       => 'image_advanced',
       		'id'         => 'id_'.$post_id . '_color_' . $color->slug,
       		'name'       => 'Tilføj billeder',
       		'max_status' => false,
       		],
       	],
         ];
       }
       ```
   

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

 *  Plugin Support [longnguyen](https://wordpress.org/support/users/longnguyen/)
 * (@longnguyen)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/add-fields-with-loop/#post-13487648)
 * Hi,
 * Please share full the code that you use to create the field, include the variable
   $colors and $post_id. I’ll run it on my end to check it out.
 * If it’s too long, you can use Pastebin to share the code [https://pastebin.com/](https://pastebin.com/).
 *  Thread Starter [pstidsen](https://wordpress.org/support/users/pstidsen/)
 * (@pstidsen)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/add-fields-with-loop/#post-13494109)
 * Hi there,
    Thanks for your quick reply.
 * I have pasted the full function here: [https://pastebin.com/QySc6tBG](https://pastebin.com/QySc6tBG)
   –
   It includes two different methods (one with one single meta box and one with 
   separate boxes – but none of them works).
 * The variable $colors is dynamically created based on the $post_id. But it could
   look like this: [https://pastebin.com/fzZdm4iJ](https://pastebin.com/fzZdm4iJ)
 * And as I said: The code shows the fields correctly but the data is not saved 
   to the post.
 *  Plugin Support [longnguyen](https://wordpress.org/support/users/longnguyen/)
 * (@longnguyen)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/add-fields-with-loop/#post-13506544)
 * Hi,
 * I’m not an expert on WooCommerce developer but checking the object $product with
   the class WC_Product and return a null value when not getting post ID will break
   the function to save the field value.
 * I’ve commented on some issue on your code then the data is saved as well.
    [https://pastebin.com/CF7bhMbm](https://pastebin.com/CF7bhMbm)
 *  Thread Starter [pstidsen](https://wordpress.org/support/users/pstidsen/)
 * (@pstidsen)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/add-fields-with-loop/#post-13514280)
 * Aah great! That did the trick 🙂
 * Thank you so much for your help! 🙂

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

The topic ‘Add fields with loop’ is closed to new replies.

 * ![](https://ps.w.org/meta-box/assets/icon-128x128.png?rev=1100915)
 * [Meta Box](https://wordpress.org/plugins/meta-box/)
 * [Support Threads](https://wordpress.org/support/plugin/meta-box/)
 * [Active Topics](https://wordpress.org/support/plugin/meta-box/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/meta-box/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/meta-box/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [pstidsen](https://wordpress.org/support/users/pstidsen/)
 * Last activity: [5 years, 8 months ago](https://wordpress.org/support/topic/add-fields-with-loop/#post-13514280)
 * Status: resolved