• Resolved pstidsen

    (@pstidsen)


    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

    (@longnguyen)

    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/.

    Thread Starter pstidsen

    (@pstidsen)

    Hi there,
    Thanks for your quick reply.

    I have pasted the full function here: 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

    And as I said: The code shows the fields correctly but the data is not saved to the post.

    Plugin Support longnguyen

    (@longnguyen)

    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

    Thread Starter pstidsen

    (@pstidsen)

    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.