title: WordPress SQL Custom Query
body: <p>WordPress SQL Custom Query To Get Post, Post Meta Data, Category and Featured Image of the post. </p>
  
  <p>I've tried so far the below code and got the post and post meta data. Now taxonomy and featured image remaining:</p>
  
  <pre><code>$query = mysql_query("
  SELECT wp_posts.ID
       , wp_posts.post_title
       , mt1.meta_value as latitude
       , mt2.meta_value as longitude
       , mt3.meta_value as full_adddress
  FROM wp_posts
  LEFT JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id AND mt1.meta_key='lv_listing_lat')
  LEFT JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id AND mt2.meta_key='lv_listing_lng')
  LEFT JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id AND mt3.meta_key='_address')
  WHERE wp_posts.post_type = 'lv_listing'
  AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
  AND ((mt1.meta_key = 'lv_listing_lat') OR (mt2.meta_key = 'lv_listing_lng' ) OR (mt3.meta_key = '_address' ))
  GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC ");
  </code></pre>
  
  <p>Revised code by myself. In this code category achieved, now only featured image attachment url needed.</p>
  
  <pre><code>SELECT DISTINCT
  ID, post_title
  , post_content
  ,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'lv_listing_lat' AND wp_postmeta.post_id = wp_posts.ID) AS "lv_listing_lat"
  ,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'lv_listing_lng' AND wp_postmeta.post_id = wp_posts.ID) AS "lv_listing_lng"
  ,(SELECT group_concat(wp_terms.name separator ', ') 
      FROM wp_terms
      INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
      INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
      WHERE taxonomy= 'listing_category' and wp_posts.ID = wpr.object_id
  ) AS "Listing Category"
  ,(SELECT group_concat(wp_terms.name separator ', ') 
      FROM wp_terms
      INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
      INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
      WHERE taxonomy= 'listing_location' and wp_posts.ID = wpr.object_id
  ) AS "Listing Location"
  FROM wp_posts
  WHERE post_type = 'lv_listing' 
  ORDER BY
  post_title
  , post_content
  </code></pre>