Featured post queries

Get all posts that have featured images that aren’t attached to the post:
SELECT post.id, post.post_name FROM `wp_posts` post, wp_posts attach, wp_postmeta meta where meta.meta_key=’_thumbnail_id’ and meta.post_id=post.id and attach.id =
meta.meta_value and attach.post_parent != post.id

Use the first attached image as featured image:
INSERT INTO `wp_postmeta`
(`post_id`,
`meta_key`,
`meta_value`)
SELECT id,
‘_thumbnail_id’,
(SELECT id
FROM wp_posts b
WHERE a.id = b.post_parent
AND post_type = ‘attachment’
LIMIT 1) AS url
FROM `wp_posts` a
LEFT JOIN wp_postmeta
ON id = post_id
AND wp_postmeta.meta_key = ‘_thumbnail_id’
WHERE wp_postmeta.meta_id IS NULL
AND post_type = ‘post’
AND post_status = ‘publish’
AND post_date <= Subdate(Now(), INTERVAL 2 year) AND (SELECT guid FROM wp_posts b WHERE a.id = b.post_parent AND post_type = 'attachment' LIMIT 1) IS NOT NULL Move featured images not attached to featuring post: update wp_posts a, wp_posts p, wp_postmeta m set a.post_parent = p.id where p.id = m.post_id and meta_key='_thumbnail_id' and meta_value=a.id and p.id != a.post_parent;

Leave a Reply

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