wp MySQL Query

I have sample PHP code from the developer based on his functions.php

$sql = “SELECT $wpdb->posts.* FROM $wpdb->posts “;
$sql .= “INNER JOIN $wpdb->term_relationships as trs ON ($wpdb->posts.ID = trs.object_id) “;
$sql .= “INNER JOIN $wpdb->term_taxonomy as tr ON (trs.term_taxonomy_id = tr.term_taxonomy_id) “;
$sql .= “INNER JOIN $wpdb->terms as trm ON (trm.term_id = tr.term_id)”;
$sql .= “INNER JOIN $wpdb->postmeta as ft ON ($wpdb->posts.ID = ft.post_id) “;
$sql .= “INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) “;
$sql .= $join;
$sql .= “WHERE 1=1 “;
$sql .= “AND tr.taxonomy = ‘activity-category’ “;
$sql .= “AND tr.term_id = “.$data[‘category’].” “;
$sql .= $where;
$sql .= “AND $wpdb->posts.post_type = ‘pt-activities-await’ “;
$sql .= “AND $wpdb->posts.post_status = ‘publish’ “;

SQL Code here:

SELECT wp_posts.* FROM wp_posts
INNER JOIN wp_term_relationships as trs ON (wp_posts.ID =
INNER JOIN wp_term_taxonomy as tr ON (trs.term_taxonomy_id = tr.term_taxonomy_id)
INNER JOIN wp_terms as trm ON (trm.term_id = tr.term_id)
INNER JOIN wp_postmeta as mt1 ON (wp_posts.ID = mt1.post_id)
INNER JOIN wp_postmeta as mt2 ON (wp_posts.ID = mt2.post_id)
INNER JOIN wp_postmeta as mt3 ON (wp_posts.ID = mt3.post_id)
INNER JOIN wp_postmeta as mt4 ON (wp_posts.ID = mt4.post_id AND mt4.meta_key = ‘start_date’)
INNER JOIN wp_postmeta as mt5 ON (wp_posts.ID = mt5.post_id AND mt5.meta_key = ‘is_featured’)
INNER JOIN wp_term_relationships as trs1 ON (wp_posts.ID = trs1.object_id)
INNER JOIN wp_term_taxonomy as tr1 ON (trs1.term_taxonomy_id = tr1.term_taxonomy_id)
INNER JOIN wp_terms as trm1 ON (trm1.term_id = tr1.term_id)
INNER JOIN wp_term_relationships as trs2 ON (wp_posts.ID = trs2.object_id)
INNER JOIN wp_term_taxonomy as tr2 ON (trs2.term_taxonomy_id = tr2.term_taxonomy_id)
INNER JOIN wp_terms as trm2 ON (trm2.term_id = tr2.term_id)
INNER JOIN wp_term_relationships as trs3 ON (wp_posts.ID = trs3.object_id)
INNER JOIN wp_term_taxonomy as tr3 ON (trs3.term_taxonomy_id = tr3.term_taxonomy_id)
INNER JOIN wp_terms as trm3 ON (trm3.term_id = tr3.term_id)
INNER JOIN wp_term_relationships as trs4 ON (wp_posts.ID = trs4.object_id)
INNER JOIN wp_term_taxonomy as tr4 ON (trs4.term_taxonomy_id = tr4.term_taxonomy_id)
INNER JOIN wp_terms as trm4 ON (trm4.term_id = tr4.term_id)
INNER JOIN wp_term_relationships as trs5 ON (wp_posts.ID = trs5.object_id)
INNER JOIN wp_term_taxonomy as tr5 ON (trs5.term_taxonomy_id = tr5.term_taxonomy_id)
INNER JOIN wp_terms as trm5 ON (trm5.term_id = tr5.term_id)
INNER JOIN wp_term_relationships as trs6 ON (wp_posts.ID = trs6.object_id)
INNER JOIN wp_term_taxonomy as tr6 ON (trs6.term_taxonomy_id = tr6.term_taxonomy_id)
INNER JOIN wp_terms as trm6 ON (trm6.term_id = tr6.term_id)
INNER JOIN wp_term_relationships as trs7 ON (wp_posts.ID = trs7.object_id)
INNER JOIN wp_term_taxonomy as tr7 ON (trs7.term_taxonomy_id = tr7.term_taxonomy_id)
INNER JOIN wp_terms as trm7 ON (trm7.term_id = tr7.term_id)
INNER JOIN wp_term_relationships as trs8 ON (wp_posts.ID = trs8.object_id)
INNER JOIN wp_term_taxonomy as tr8 ON (trs8.term_taxonomy_id = tr8.term_taxonomy_id)
INNER JOIN wp_terms as trm8 ON (trm8.term_id = tr8.term_id)
INNER JOIN wp_term_relationships as trs9 ON (wp_posts.ID = trs9.object_id)
INNER JOIN wp_term_taxonomy as tr9 ON (trs9.term_taxonomy_id = tr9.term_taxonomy_id)
INNER JOIN wp_terms as trm9 ON (trm9.term_id = tr9.term_id)
INNER JOIN wp_term_relationships as trs10 ON (wp_posts.ID = trs10.object_id)
INNER JOIN wp_term_taxonomy as tr10 ON (trs10.term_taxonomy_id = tr10.term_taxonomy_id)
INNER JOIN wp_terms as trm10 ON (trm10.term_id = tr10.term_id) WHERE 1=1 AND tr1.taxonomy = ‘event-tags’ AND trm1.slug = ‘entertainment’
AND tr2.taxonomy = ‘event-tags’
AND trm2.slug = ‘family’
AND tr3.taxonomy = ‘event-tags’
AND trm3.slug = ‘museum-history’
AND tr4.taxonomy = ‘event-tags’
AND trm4.slug = ‘galleries-visual-arts’
AND tr5.taxonomy = ‘event-tags’
AND trm5.slug = ‘music-concerts’
AND tr6.taxonomy = ‘event-tags’
AND trm6.slug = ‘featured-events’
AND tr7.taxonomy = ‘event-tags’
AND trm7.slug = ‘sporting’
AND tr8.taxonomy = ‘event-tags’
AND trm8.slug = ‘festivals-events’
AND tr9.taxonomy = ‘event-tags’
AND trm9.slug = ‘performing-arts’
AND tr10.taxonomy = ‘event-tags’
AND trm10.slug = ‘culinary’
AND wp_posts.post_type = ‘pt-event’
AND wp_posts.post_status = ‘publish’
AND mt3.meta_key = ‘expiration_date’
AND CAST( mt3.meta_value AS DATE ) > curdate()
AND ( (mt1.meta_key = ‘start_date’
AND MONTH( CAST( mt1.meta_value AS DATE ) ) <= 09) A ND (mt2.meta_key = 'end_date' AND MONTH( CAST( mt2.meta_value AS DATE ) ) >= 09) )
GROUP BY wp_posts.ID
ORDER BY mt5.meta_value DESC, mt4.meta_value ASC

wordpress , MySQL , Oracol