WordPress.org

Ready to get started?Download WordPress

Forums

Adding attachment to post using wp_insert_post (3 posts)

  1. wheremyfilesat.com
    Member
    Posted 2 years ago #

    Hi all,
    couple weeks ago i got my first wordpress site up and running, now im trying to make a custom home page for it.

    I have a bash script on my server that produces a php page to call wordpresses wp_insert_post function, this works fine.

    However, im using a plugin on my home page to list post from specific categories, this plugin inserts a thumbnail next to the the post.

    but im having trouble adding a thumbnail to my post, using the script. if i create a post manually and choose the 'set featured image' button on the post creation page my thumbnail shows up.

    Heres what i have so far, this will insert the image into the wordpress media gallery, but i cant get it to add to the post.

    <?php
      require_once(ABSPATH . 'wp-includes/functions.php');
      require_once(ABSPATH . 'wp-admin/includes/image.php');
      include_once"wp-config.php";
      include_once"wp-load.php";
      include_once"wp-includes/wp-db.php";
    
      $filename = "http://188.72.223.152/wheremyfilesat/wp-content/uploads/xelb1cnn.jpg";
      $wp_filetype = wp_check_filetype(basename($filename), null );
      $attachment = array(
         'post_mime_type' => $wp_filetype['type'],
         'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
         'post_content' => '',
         'post_status' => 'inherit'
      );
      $attach_id = wp_insert_attachment( $attachment, $filename, 866 );
    
      $thumb_attach_attr = wp_get_attachment_image_src( $attach_id );
    
      $postmeta_id = add_post_meta(866, 'thumbnail', $thumb_attach_attr[0]);
    
      // you must first include the image.php file
      // for the function wp_generate_attachment_metadata() to work
      $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
      wp_update_attachment_metadata( $attach_id, $attach_data );
    ?>

    866 is the id of the post i want the thumbnail inserted to

    Ive almost have my page setup the way i want, its this little thing that is driving me crazy
    thanks

  2. refulez
    Member
    Posted 2 years ago #

  3. wheremyfilesat.com
    Member
    Posted 2 years ago #

    omg thnx so much, that had been driving me crazy for sooo long

    i was missing one simple line
    set_post_thumbnail( 866, $attach_id );

    i must have missed that on the codex somehow

    here my final code incase it helps anyone

    $post_id = 866; // this value is retrived with '$post_id = wp_insert_post(....)'
      $filename = "http://www.wheremyfilesat.com/wp-content/uploads/xelb1cnn.jpg";
      $wp_filetype = wp_check_filetype(basename($filename), null );
      $attachment = array(
         'post_mime_type' => $wp_filetype['type'],
         'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
         'post_content' => '',
         'post_status' => 'inherit'
      );
    
      $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
    
      $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
      wp_update_attachment_metadata( $attach_id, $attach_data );
    
      set_post_thumbnail( $post_id, $attach_id );

    Thanks again

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.