I am migrating this site where I have a lot of posts without featured images on them. I started doing this per page on the interface but its taking forever. I thought a using database insert might go faster.
The posts do not have an image attached to them so I can’t use the plugins that would automatically set the first image as featured.
Have any of you done this before or could point me to resources?
I found this question interesting, and as I need to insert images in wp using directly sql I investigated some more.
First of all you need to have a post or a page to assign the featured image to. I assume you inserted a post by way of the wp interface, or calling wp_insert_post() or directly by sql inserting into $wp->post. Then you need the ID of this post, say “target_post_id”.
Now you need to have an image inserted in the database. Once again you can use anyone of the previous method but to be fast I load all my pre-resized images in a and insert them using sql:
Last step is to bind the post to the image, as answered by cr8ivecodesmith:
there are some not null fields that wp db will fill with default values
I was actually able to make it work after observing the data from the records at hand. I only had to work with the
wp_postmeta
table using the query:Hope this helps other guys out there.
step #1 insert a record on you wp_posts table for your attahcment
$filname = url of the image you want to use as thumbnail e.g. http://yourdomain.com/wp-content/uploads/2014/09/thumbnail.jpg
$parentpostid = post_id of the post you want to attache your thumbnail to.
step #2 create a post meta record on wp_postmeta for your attachement
$filename = the filename of your file. e.ge. 2014/09/thumbnail.jpg
$attachmentid = post_id of the attachement post created on step #1
step #3 create a post meta record to identify the attachement post as the thumbnail for the post you are attaching your thumbnail into.
$parentpostid = the post_id of the post you want your thumbnail to be attached to.
$attachmentid = the post_id of the attachment post created on step #1