title: How to merge two SQL queries to get latest Wordpress Post and Featured Image
body: <p>I'm trying to show latest Post Excerpt, Post Title and Featured Image on an ASP page. To simplify the query I add the Permalink as a custom field for each Post. I have a query that gets all except Featured Image and I have another query that gets Featured image but I can't work out how to merge them into one query.</p>
  
  <p>// Gets Post Excerpt and Post Title</p>
  
  <pre><code>SELECT
   wp_posts.id, 
   wp_posts.post_title, 
   wp_postmeta.meta_value, 
   wp_postmeta.meta_key, 
   wp_posts.post_excerpt 
   FROM 
   wp_postmeta 
   INNER JOIN wp_posts p ON (wp_postmeta.post_id = wp_posts.ID) 
   WHERE post_id IN (
   SELECT wp_posts.id 
   FROM wp_posts 
   WHERE post_type = 'post' 
   AND post_status = 'publish' 
   AND meta_key = 'my_permalink' 
   ORDER BY post_date, wp_posts.id
   ) 
   ORDER BY wp_posts.post_date DESC, wp_postmeta.post_id 
   LIMIT 2
  </code></pre>
  
  <p>// Gets Featured Images for a Post</p>
  
  <pre><code> SELECT p.*
        FROM wp_postmeta AS pm
       INNER JOIN wp_posts AS p ON pm.meta_value=p.ID 
       WHERE pm.post_id = $ID
         AND pm.meta_key = '_thumbnail_id' 
       ORDER BY p.post_date DESC 
  </code></pre>
  
  <p>Can anyone help me merge these queries? Thanks.</p>
  
  <p>Sample data to be returned:
  ID | post_title | post_excerpt | meta_value_my_permalink | featured_image_guid</p>
  
  <p>** UPDATE <em>*</em>
  I've managed to get the following which works fine except I can't get more that one row as I get an error when I try and use 'IN' in a subquery
  e.g. pm2.post_id IN (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 2)</p>
  
  <pre><code>SELECT 
    p.post_title,
    p.post_excerpt,
    pm.meta_value AS permalink,
    p2.guid as thumbnail,
    p2.post_title as image_alt
  FROM
    wp_postmeta pm
    INNER JOIN wp_posts p ON (pm.post_id = p.ID),
    wp_postmeta pm2
    INNER JOIN wp_posts p2 ON (pm2.meta_value = p2.ID)
  WHERE
    pm.post_id = (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1) AND 
    pm.meta_key = 'my_permalink' AND 
    pm2.post_id = (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1) AND 
    pm2.meta_key = '_thumbnail_id'
  </code></pre>