I’ve made a quick PHP script/page that uses the wp_insert_post() function to insert a post directly. And this works great. The post I am manually adding has an image in it, so I firstly manually/programatically upload the image to the respective “/uploads/” folder where all my post images are saved (btw im not using year/month sub-directories). The content of the post has normal text, plus the necessary HTML to point to the image too, eg.

<p>This is a manually created post for my blog, there should be a picture below this line of text.  Thanks for visiting.</p>
<img src="https://www.mysite.com/wp-content/uploads/image.jpg">

The post appears fine (including the image). But my 2 problems are:

1) The image I uploaded is not in the Media Library, How do I get it in there?

2) I would like to add a “featured image/thumbnail” to the same image, but I dont know what function is used to make that call? I was hoping to find a function along the lines of insert_featured_thumbnail(“/wp-content/uploads/image.jpg”, $pid)

Does anyone have any ideas?

Thanks in advanced.

WizzKidd

2 Answers
2

Function Reference/wp insert attachment

$image_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

This function will return an image id which you can use for the featured image.

update_post_meta($post_id, "_thumbnail_id", $image_id);

Leave a Reply

Your email address will not be published. Required fields are marked *