title: wordpress attaching gallery photos to new post by coding
body: <p>I am doing a migration of articles from drupal to wordrepss, there are articles with gallery, I have not yet found how to link a gallery of multiple photos to an article</p>
  
  <p>Can you please help me pass this step</p>
  
  <p>Here is my code :</p>
  
  <pre><code>.....
  $my_post        //A table that I filled with the necessary elements
  $post_id        =   wp_insert_post( $my_post );
  
  // Create the attachment of main photo
  // Set attachment data
  $attachment = array(
                      'post_mime_type' =&gt; $wp_filetype['type'],
                      'post_title'     =&gt; sanitize_file_name( $filename ),
                      'post_content'   =&gt; '',
                      'post_status'    =&gt; 'inherit'
                  );
  $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
  
  //insertion of photo gallery
  set_post_format($post_id, 'gallery' );
  for($n=1;$n&lt;=$NbrImgGallery;$n++){
         $my_post_gallery['post_title']       =   "Titre photo-".$n;
          $my_post_gallery['post_content']    =   "Description photo-".$n;
          $my_post_gallery['post_status']     =   'publish';
          $my_post_gallery['post_author']     =   1;      
          $my_post_gallery['post_type']       =   "attachment";
          $my_post_gallery['post_parent']     =   $post_id;
          $Gallery_post_id        =   wp_insert_post( $my_post_gallery );
  }
  </code></pre>