posts.* FROM $wpdb->posts,-6ren">
gpt4 book ai didi

mysql - WordPress 加入 wp_post 和 wp_postmeta

转载 作者:行者123 更新时间:2023-11-29 05:30:32 36 4
gpt4 key购买 nike

我正在尝试使用此查询同时搜索 wp_post 和 wp_postmeta:

$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=Vancouver 作为其元字段的帖子。但是我要如何再添加一个条件才能使不列颠哥伦比亚省的温哥华和华盛顿州的温哥华不在一起?

类似于:

    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 个元值和 1 个自定义分类法搜索自定义 posts_types。

所以我的结构是:

  1. 每个自定义职位(人才)都有 3 个自定义字段(城市、国家/地区、性别)。
  2. 每个自定义帖子都属于自定义分类法“语言”,具有特定类别,例如英语、西类牙语、俄语等。

所以下面的查询是在加拿大多伦多寻找一位会说法语的女性。

$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";

关于mysql - WordPress 加入 wp_post 和 wp_postmeta,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15243114/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com