我正在尝试使用以下查询同时搜索wp-post和wp-postteta:

$querystr = "
    SELECT $wpdb->posts.*
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
    AND $wpdb->postmeta.meta_key = 'City'
    AND $wpdb->postmeta.meta_value = 'Vancouver'
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'talents'
    ORDER BY $wpdb->posts.post_date DESC
 ";

$pageposts = $wpdb->get_results($querystr, OBJECT);

如您所见,我搜索的帖子中包含City=wincover作为其元字段。但我该如何再增加一个条件,这样我就不会把温哥华,不列颠哥伦比亚省和温哥华,华盛顿州联系在一起了?
smth类:
    AND ($wpdb->postmeta.meta_key = 'Country'
    AND $wpdb->postmeta.meta_value = 'Canada')
    AND ($wpdb->postmeta.meta_key = 'City'
    AND $wpdb->postmeta.meta_value = 'Vancouver')

最佳答案

基于上面提到的链接,我能够创建这个模式来搜索基于3个meta_值和1个自定义分类法的自定义posts_类型。
所以我的结构是:
每个定制岗位(人才)有3个定制字段(城市、国家、性别)。
每个自定义贴子都属于自定义分类“语言”,具有某些类别,如英语、西班牙语、俄语等。
所以下面的问题是在加拿大多伦多寻找一位会说法语的女性。

$querystr= "SELECT * FROM wp_posts
LEFT JOIN wp_postmeta v1 ON (wp_posts.ID = v1.post_id)
LEFT JOIN wp_postmeta v2 ON (wp_posts.ID = v2.post_id)
LEFT JOIN wp_postmeta v3 ON (wp_posts.ID = v3.post_id)
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms ON (wp_term_taxonomy.term_id = wp_terms.term_id)
WHERE
wp_terms.name = 'French' AND wp_term_taxonomy.taxonomy = 'Languages' AND wp_posts.post_status = 'publish' AND wp_posts.post_type = 'talents'
AND v1.meta_value = 'Toronto'
AND v2.meta_value = 'Canada'
AND v3.meta_value = 'female'
ORDER BY wp_posts.post_date DESC";

10-08 19:13