SELECT SQL_CALC_FOUND_ROWS wp_posts.*
FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT JOIN (SELECT * FROM wp_postmeta WHERE meta_key = '_sale_price' AND meta_value > 0) sale ON (wp_posts.ID = sale.post_id) LEFT JOIN (SELECT * FROM wp_postmeta WHERE meta_key = '_stock_status') stock ON (wp_posts.ID = stock.post_id)
WHERE 1=1 AND (
wp_posts.ID NOT IN (
SELECT object_id
FROM wp_term_relationships
WHERE term_taxonomy_id IN (7)
)
AND
wp_term_relationships.term_taxonomy_id IN (161)
) AND (
wp_postmeta.meta_key = 'select_maker'
) AND ((wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'wc-fraud-screen' OR wp_posts.post_status = 'wc-authorised')))
GROUP BY wp_posts.ID
ORDER BY stock.meta_value ASC, sale.meta_value DESC, wp_postmeta.meta_value ASC
LIMIT 0, 1500