Skip to content

Instantly share code, notes, and snippets.

@michael-picard
michael-picard / assign_first_post_image_as_featured_image.sql
Last active April 21, 2017 07:35
This query is looking for the image id attached at the beginning of a post and set it as featured image of the post.
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID, '_thumbnail_id', substr(post_content, locate('wp-image-', post_content) + 9, 4) FROM wp_posts AS p
LEFT JOIN (SELECT * FROM wp_postmeta WHERE meta_key = '_thumbnail_id') AS m ON p.ID = m.post_id
WHERE p.post_type = 'post' AND p.post_status = 'publish' AND left(p.post_content, 100) LIKE '%wp-image-%' AND
meta_value IS NULL;