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

Viewing 8 replies - 1 through 8 (of 8 total)
  • 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.

    Thread Starter jfigaro

    (@jfigaro)

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

    url should be no problem, as $filename is just a path to the image. make it absolute with https:// 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 ??

    why not use a “featured image” ?

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

    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.

    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.

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

    Example on bottom of this page with correct code to use:
    https://codex.www.ads-software.com/Function_Reference/wp_insert_attachment

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘programmatically create posts with a post thumbnail?’ is closed to new replies.