How to merge select queries from wp_term column?

I am returning attributes values from wp_term and this is one of my queries for one attribute, say color. And I have the same queries for other attributes. I am looking for a way to merge them all in query since I couldn’t get it to work with multiple mysqli function to output them. Is there anyway to merge these queries:

$sql = “SELECT p.`ID` AS ‘Product ID’,
p.`post_title` AS ‘Product Name’,
t.`term_id` AS ‘Attribute Value ID’,
REPLACE(REPLACE(tt.`taxonomy`, ‘pa_’, ”), ‘-‘, ‘ ‘) AS ‘Attribute Name’,
t.`name` AS ‘Carat’
FROM `wp_posts` AS p
INNER JOIN `wp_term_relationships` AS tr ON p.`ID` = tr.`object_id`
INNER JOIN `wp_term_taxonomy` AS tt ON tr.`term_taxonomy_id` = tt.`term_id` AND tt.`taxonomy` LIKE ‘pa_carat%’
INNER JOIN `wp_terms` AS t ON tr.`term_taxonomy_id` = t.`term_id`
WHERE p.`post_type` = ‘product’
AND p.`post_status` = ‘publish’ ORDER BY p.`ID`”;

$sql .= ”
SELECT p.`ID` AS ‘Product ID’,
p.`post_title` AS ‘Product Name’,
t.`term_id` AS ‘Attribute Value ID’,
REPLACE(REPLACE(tt.`taxonomy`, ‘pa_’, ”), ‘-‘, ‘ ‘) AS ‘Attribute Name’,
t.`name` AS ‘Color’
FROM `wp_posts` AS p
INNER JOIN `wp_term_relationships` AS tr ON p.`ID` = tr.`object_id`
INNER JOIN `wp_term_taxonomy` AS tt ON tr.`term_taxonomy_id` = tt.`term_id` AND tt.`taxonomy` LIKE ‘pa_color%’
INNER JOIN `wp_terms` AS t ON tr.`term_taxonomy_id` = t.`term_id`
WHERE p.`post_type` = ‘product’
AND p.`post_status` = ‘publish’ ORDER BY p.`ID`”;

Read more here:: How to merge select queries from wp_term column?

Leave a Reply

Your email address will not be published. Required fields are marked *