Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ibrahimkholil/20bf23de652acbe0b695702029d1a0d6 to your computer and use it in GitHub Desktop.
Save ibrahimkholil/20bf23de652acbe0b695702029d1a0d6 to your computer and use it in GitHub Desktop.
wordpress meta key and values SQL Query
hi guys,
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.
Let me show u what I am trying to achieve:
go to this page
http://jollyhits.com/classiads/test-list/4
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
";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment