Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save michael-picard/d2b3d00c0eecfc3e110be791cc367996 to your computer and use it in GitHub Desktop.
Save michael-picard/d2b3d00c0eecfc3e110be791cc367996 to your computer and use it in GitHub Desktop.
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment