I am trying to manually query from an external php file the wordpress database and I want to extract the last 3 posts with: title, content, last (or first) image, and for that image, from wp_postmeta I want to get the thumbnail url.
I managed to get the title, content, image id, but I couldn’t figure it out how to add another join for getting image thumbnail. This is what I have:
SELECT a.post_title title, max(c.guid) img_url, a.ID id
FROM wp_posts a
LEFT JOIN
(select post_parent, max(post_date_gmt) as latest_image_date from wp_posts
where post_type='attachment' GROUP BY post_parent) b
on a.id=b.post_parent
LEFT JOIN wp_posts c
on c.post_parent=a.id
and c.post_type='attachment'
and b.latest_image_date = c.post_date_gmt where c.guid IS NOT NULL
GROUP BY a.post_title ORDER BY a.ID
The image thumbnail is in wp_postmeta (meta_id, post_id, meta_key, meta_value) table, looking like this: 58435, 6711, _wp_attachment_metadata, a:6:{s:5:"width";s:4:"1024";s:6:"height";s:3:"683"...
I could see I get the image id in c.id and all it needs is another JOIN to also get data from wp_postmeta for the field where meta_key="_wp_attachment_metadata" and post_id=c.id.
Can anyone help me with completing the query? Thanks!
In the end I chose another solution (at least temporary) because it adds horizontal capabilities: I created a completely empty custom page (I had to manually remove some actions/filters to get it empty) in the other site (the one to be included) which exports data in json format. I get that data and print it as I need in the current site.