• Hello

    I’m creating a custom post submit form for wordpress and at the moment I can create the post and have the image uploaded and attached to the post but the next step im struggling at – I’m looking to have the uploaded image automatically displayed on the post (as image), ive looked into the featured image option but just can’t get it to work

    Any help is appreciated

    $my_post = array();
    $my_post[‘post_title’] = $newid;
    $my_post[‘post_content’] = ”;
    $my_post[‘post_author’] = 1;
    $my_post[‘post_status’] = draft;
    $my_post[‘post_category’] = array($a);
    // Insert the post into the database
    $post_id = wp_insert_post($my_post);
    //
    $wp_filetype = wp_check_filetype(basename($id . “.gif”), null );
    $attachment = array();
    $attachment[‘post_mime_type’] = $wp_filetype[‘type’];
    $attachment[‘post_title’] = $newid;
    $attachment[‘post_content’] = ”;
    $attachment[‘post_status’] = ‘inherit’;
    $attach_id = wp_insert_attachment($attachment, $id . “.gif”, $post_id)
    require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);
    $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
    wp_update_attachment_metadata($attach_id, $attach_data);
    set_post_thumbnail($post_id, $attach_id);
    //
    wp_redirect( site_url().”?p=$post_id” ); exit();

    Thanks for looking

The topic ‘Using set_post_thumbnail in custom template’ is closed to new replies.