I’m using this query in a PHP script outside WordPress to retrieve entries with their featured images
SELECT ( SELECT guid FROM wp_posts WHERE id = m.meta_value ) AS url
FROM wp_posts p, wp_postmeta m
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND p.id = m.post_id
AND m.meta_key = '_thumbnail_id'
…and it works fine.
But this way I get full-size image URL. I need to retrieve ‘medium’ or ‘thumbnail’ sizes of these images.
¿Any way to achieve this?
here is the response :
The following query, adapted from the above, solved my particular problem which was simply to grab the last four posts and their featured images. Plus the post_name from which I could construct a pretty URL
Of course from there it’s easy to make an excerpt etc using:
You can try this query for thumbnail size , for medium image i am not sure about the right size if you know the dimension then make custom alias as i made below using the SUBSTRING_INDEX to get the extension of file then i have used CONCAT function with the
post_name
column and the dimensions + extension ,similarly you can do this for medium size , As all upload goes to the upload folder you can analyze the generated thumbs name areoriginal attachment name + -150x150 or other dimensions
so from this logic your thumbs get the name with the dimensions, the attachments of post are stored in post_meta with the post id and having key name _wp_attachment_metadata which stores all the information about different sizes of file but in a serialized form so in mysql query you cannot unserialize the dataThis query works for me to get thumbnail of size 150*150 hope it works for you also
And then use unserialize PHP function with meta_value