title: How to manually query posts title, content and image attachment thumbnail?
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The image thumbnail is in <code>wp_postmeta (meta_id, post_id, meta_key, meta_value)</code> table, looking like this: <code>58435, 6711, _wp_attachment_metadata, a:6:{s:5:"width";s:4:"1024";s:6:"height";s:3:"683"...</code></p>
  
  <p>I could see I get the image id in <code>c.id</code> and all it needs is another <code>JOIN</code> to also get data from <code>wp_postmeta</code> for the field where <code>meta_key="_wp_attachment_metadata" and post_id=c.id</code>.</p>
  
  <p>Can anyone help me with completing the query? Thanks!</p>