WordPressで2つ投稿タイプの条件に合致する投稿を取得する

とりあえず以下の記述で取得できたのでメモしておきます。

post_type01→一つ目のポストタイプ
post_type02→二つ目のポストタイプ
meta_key1→二つ目のポストタイプのカスタムフィールドのキー1
meta_key2→二つ目のポストタイプのカスタムフィールドのキー2

  global $wpdb;

//SQLに挿入する変数
  $search_key_leases = array('value1','value2','value3', $min_price, $max_price);

  $sql = $wpdb->prepare("
SELECT ID as lease_id, lm1.meta_value AS value1 , lm2.meta_value AS value2 , lm3.meta_value AS value3
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta AS lm1 ON (ID = lm1.post_id AND lm1.meta_key = %s)
LEFT JOIN $wpdb->postmeta AS lm2 ON (ID = lm2.post_id AND lm2.meta_key = %s)
LEFT JOIN $wpdb->postmeta AS lm3 ON (ID = lm3.post_id AND lm3.meta_key = %s)
WHERE post_type = 'post_type01'
AND post_status = 'publish'
AND lm3.meta_value IS NULL
AND lm2.meta_value <= CURDATE()
AND lm1.meta_value IN (SELECT ID
    FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta AS pm1 ON (ID = pm1.post_id AND pm1.meta_key = 'meta_key1')
    LEFT JOIN $wpdb->postmeta AS pm2 ON (ID = pm2.post_id AND pm2.meta_key = 'meta_key2')
    WHERE post_type = 'post_type02'
    AND post_status = 'publish'
    AND pm1.meta_value = '合致させたい内容'
    AND pm2.meta_value BETWEEN %d AND %d
    )", $search_key_leases);

  $results = $wpdb->get_results($sql, ARRAY_A);

  if($results) {
    foreach ($results as $result) {
      $value_ids[] = $result['value1'];
    }
  }