I’m sure there’s a better approach, but this query should do what you want:
SELECT DISTINCT meta_value FROM wp_postmeta pm
WHERE meta_key = 'Driver'
AND NOT EXISTS (SELECT * FROM wp_term_relationships tr
JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
JOIN wp_terms t ON t.term_id = tt.term_id
WHERE pm.post_id = tr.object_id
AND tt.taxonomy = 'category'
AND t.name="ExcludeMeCategory");
When translating this into php you should use the $wpdb
aliases for table names, i.e. $wpdb->postmeta
, $wpdb->term_relationships
, $wpdb->term_taxonomy
, $wpdb->terms
. You should also probably profile this query to make sure it’s executing in reasonable time and isn’t doing full table scans, etc. – I don’t a representative database handy to test it.
Finally I don’t know taxonomies well: this may be incomplete w.r.t. e.g. taxonomy hierarchies, and it may be simpler / better to query the taxonomy ID first and then drop that into the query in place of wp_terms_taxonomy
onwards in the query.