title: Attaching a thumbnail while using wp_insert_post() in WordPress
body: <p>I have a script that creates posts using wp_insert_post(). Every day the script creates 12 posts, and the posts have the same names. 
  <br>
  <br>
  This is the code used:</p>
  
  <pre><code>require_once( ABSPATH . 'wp-admin/includes/post.php' );
  
  global $post;
  
      function PostCreator($title, $name, $content, $sign, $meta_input) { 
          //$postID = post_exists( $title );
              $post_data = array(
                  'post_title'    =&gt; $title,
                  'post_content'  =&gt; $content,
                  'post_status'   =&gt; 'publish',
                  'post_type'     =&gt; 'post',
                  'post_author'   =&gt; '1',
                  'post_category' =&gt; array('category' =&gt; 2),
                  'meta_input'    =&gt; $meta_input,
                  'post_name' =&gt; $name,
                  );
  
              wp_insert_post( $post_data, $error_obj );
  
              if(!isset($post))
                  add_action('admin_init', 'hbt_create_post' );
              return $error_obj;
          } 
  </code></pre>
  
  <p>What I want to do is have each post automatically attach an thumbnail from the media library (not upload a new image every time a post is created)<br>
  I have all 12 images in the media library.<br>
  My problem is everything I try seems to either not work, or try to re-upload images, which fills my server with duplicates.<br>
  <br>
  Anyone have any insight as to what I might be able to do?</p>