WordPress.org

Ready to get started?Download WordPress

Forums

programmatically create posts with a post thumbnail? (9 posts)

  1. jfigaro
    Member
    Posted 2 years ago #

    hey, guys. still attempting to assign thumbnails to posts, if anyone could help it would be appreciated.

    I'm currently retrieving listings from a feed and creating posts for each. some listings contain images; these listings will need one of the images to be set as the post thumbnail.

    do I need to save the images to the server prior to using them for thumbnails? currently, the 'src' attribute of every image is being saved into the database.

    thank you in advance.

  2. digibucc
    Member
    Posted 2 years ago #

    here is the code i use on a custom program to add an image to a post, you need the post id. you can get that when you run "wp_insert_attachment" , it returns the id.

    $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, $parent_id );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );

    if it's the first/only image attachment, it will get used as the thumbnail. if not you can assign it as one with:
    add_post_meta($post_id, '_thumbnail_id', $attach_id, true);

    though, you need to make sure it is the right size. if it's too big it will only show part of it as the thumbnail. for that, i implemented a php image resize function, which is common, google.

  3. jfigaro
    Member
    Posted 2 years ago #

    thanks digi. do I need to save images from a listing to my server, or can wp insert attachments via url?

  4. digibucc
    Member
    Posted 2 years ago #

    url should be no problem, as $filename is just a path to the image. make it absolute with http:// and there should be no problem.

    idk for sure, but i would be really surprised if it didn't simply work, if that's the case though - comment back. any number of possible fixes are coming to mind ;)

  5. yogasukma
    Member
    Posted 2 years ago #

    why not use a "featured image" ?

  6. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    @yogasukma: thumbnail IS featured image. They are one and the same. And he is trying to set the featured image through php

  7. harvitronix
    Member
    Posted 2 years ago #

    I'm using the code digibucc suggested, and it's nearly working.

    The image is being inserted into the media section, and it is being attached to the post. But it won't show up as the thumbnail/featured image.

    Here is my code:

    $cur_post_id = wp_insert_post( $my_post );
    
    $filename = "/wp/wp-content/uploads/2012/03/thumb13.png";
    $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, $cur_post_id );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );
    add_post_meta($cur_post_id, '_thumbnail_id', $attach_id, true);

    Any idea why WordPress isn't recognizing it as the thumb?

    Thanks a lot for the help!
    Matt.

  8. harvitronix
    Member
    Posted 2 years ago #

    Figured it out. For future explorers of this thread:

    $cur_post_id = wp_insert_post( $my_post );
    
    $filename = "/path/to/local/file.png";
    
    $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, $cur_post_id );
    add_post_meta($cur_post_id, '_thumbnail_id', $attach_id, true);

    Cheers,
    Matt.

  9. zschuessler
    Member
    Posted 2 years ago #

    @harvitronix - Not bad, but you aren't getting resized thumbnails with your code.

    Example on bottom of this page with correct code to use:
    http://codex.wordpress.org/Function_Reference/wp_insert_attachment

Topic Closed

This topic has been closed to new replies.

About this Topic