Adding attachment to post using wp_insert_post
-
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
The topic ‘Adding attachment to post using wp_insert_post’ is closed to new replies.