I have next code:
$drivers_all = $wpdb->get_results("SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key = 'driver'")
With this one I get the values of all fields “driver”. How can I exclude some category in this code?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.Sign up to join this community
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->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.