I am stuck in finding the way of how to make such query that will find the data from meta_key and meta_values form wordpress post meta table related with posts.
one field is category field and other fields are meta_key and values.
So, the SQL query that I am using is
$querystr="SELECT distinct wposts.*
FROM $wpdb->posts wposts
LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE ((wpostmeta.meta_key = 'Regione' AND wpostmeta.meta_value ='".$_REQUEST['cmb_search_region']."')
OR (wpostmeta.meta_key = 'Provincia' AND wpostmeta.meta_value ='".$_REQUEST['cmb_search_province']."'))
AND (wposts.post_title like '%".$_REQUEST['srch_text']."%' OR wposts.post_content like '%".$_REQUEST['srch_text']."%')
AND wposts.post_type = 'post'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id IN (".$_REQUEST['cmb_search_categories'].")
ORDER BY wpostmeta.meta_value ASC
LIMIT 4
";
SELECT posts.ID
, posts.post_title
, posts.post_content
FROM ( SELECT post_id
FROM postsmeta
WHERE meta_key = 'Regione' AND meta_value = 17
OR meta_key = 'Provincia' AND meta_value = 3
GROUP
BY post_id
HAVING COUNT(*) = 2 ) AS m
INNER
JOIN posts
ON posts.ID = m.post_id