1

我有 2 个分类法:citystreet.

我需要获得postmeta我的一个插件。

只有一种分类的工作代码:

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')

但如果我附加

AND ($wpdb->term_taxonomy.taxonomy = 'street' AND $wpdb->term_taxonomy.term_id = '5')

它返回空结果。

我也尝试这样做:

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');

它也不起作用。

请注意:我需要同时匹配两者,例如:city = 2 AND street = 2
不是:city = 2 OR street = 2

4

1 回答 1

0

您的第一次尝试将不起作用,因为单个分类记录不能同时包含街道和城市。

更新您的第二次尝试:

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');
于 2017-02-24T04:31:35.227 回答