I have 2 taxonomies: city
and street
.
I need to get postmeta
for one of my plugins.
Working code for only one taxonomy:
SELECT *
FROM $wpdb->postmeta
LEFT JOIN $wpdb->posts ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON ($wpdb->postmeta.post_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 meta_key LIKE 'rwp_rating%'
AND ($wpdb->term_taxonomy.taxonomy = 'city'
AND $wpdb->term_taxonomy.term_id = '2')
but if I append
AND ($wpdb->term_taxonomy.taxonomy = 'street' AND $wpdb->term_taxonomy.term_id = '5')
it returns empty result.
Also I tried to do that:
SELECT *
FROM $wpdb->postmeta
LEFT JOIN $wpdb->posts ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON ($wpdb->postmeta.post_id = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy AS t1 ON ($wpdb->term_relationships.term_taxonomy_id = t1.term_taxonomy_id)
LEFT JOIN $wpdb->term_taxonomy AS t2 ON ($wpdb->term_relationships.term_taxonomy_id = t2.term_taxonomy_id)
WHERE meta_key LIKE 'rwp_rating%'
AND (t1.taxonomy = 'city'
AND t1.term_id = '2')
AND (t2.taxonomy = 'street'
AND t2.term_id = '3331');
And it doesn't working too.
Please Note: I need to match both, like: city = 2 AND street = 2
not: city = 2 OR street = 2
Your first attempt will not work because a single taxonomy record can not have both street and city in it.
Update your second attempt to this :
SELECT * FROM $wpdb->postmeta
LEFT JOIN $wpdb->posts ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON ($wpdb->postmeta.post_id = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy as t1 ON ($wpdb->term_relationships.term_taxonomy_id = t1.term_taxonomy_id AND t1.taxonomy = 'city')
LEFT JOIN $wpdb->term_taxonomy as t2 ON ($wpdb->term_relationships.term_taxonomy_id = t2.term_taxonomy_id AND t2.taxonomy = 'street')
WHERE meta_key LIKE 'rwp_rating%' AND t1.term_id = '2' AND t2.term_id = '3331');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.