When I just let one WHERE and remove the other it returns me the correct result. When I have it as is it does not display anything. How can I write correctly my logic below?
My aim is to display only the result who satisfy the “extra1 = $value1 and extra2 = $value2 and extra3 = $value3.”
I read somewhere that I have to add the addslashes. What is the use of it?
$value1 = addslashes($_GET['extra1']);
$value2 = addslashes($_GET['extra2']);
$value3 = addslashes($_GET['extra3']);
$theposts = $wpdb->get_results("SELECT post_title FROM {$wpdb->posts}
WHERE ID IN
(
SELECT DISTINCT post_id
FROM {$wpdb->postmeta}
WHERE meta_key = 'extra1' AND meta_value = '$value1'
WHERE meta_key = 'extra2' AND meta_value = '$value2'
WHERE meta_key = 'extra3' AND meta_value = '$value3'
)");
If your database supports intersections then you can do this:
That will give you all the
post_ids that have extra1/value1, extra2/value2, and extra3/value3.If you don’t have
INTERSECT(such as, AFAIK, MySQL), you can tryHAVING: