My example below “obviously” doesn’t work ::: Is there a way I can get the IF ELSE or something similar to work ::: I have several PHP variables, based on these variables I’d like to change the AND “clause” in this query, without having to create several queries and using PHP if statements to use the correct query block :::
$query = "SELECT $wpdb->posts.ID
FROM $wpdb->posts
INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE 1=1
AND $wpdb->posts.post_type = 'cars'
AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private')
IF ( $drive )
AND ( ($wpdb->postmeta.meta_key = 'vehicle_year' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$year')
ELSEIF ( $fuel )
AND ( ($wpdb->postmeta.meta_key = 'vehicle_fuel' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$fuel')
ENDIF
AND ( ($wpdb->postmeta.meta_key = 'vehicle_fuel' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$fuel') )
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date
DESC LIMIT 0, 99";
1 Answer