如何在mysql中连接wp_posts和wp_postmeta?

时间:2015-06-22 09:29:22

标签: mysql wordpress

如何在MySQL中连接WordPress wp_postswp_postmeta?提前谢谢..

2 个答案:

答案 0 :(得分:2)

post_idwp_postmeta表中的索引。

更多信息in the source

答案 1 :(得分:1)

试试这个, 基于上面提到的链接,我能够创建此模式,以根据3个meta_values和1个自定义分类法搜索自定义posts_types。

所以我的结构是:每个自定义帖子(人才)都有3个自定义字段(城市,国家,性别)。     每个自定义帖子都属于自定义分类标准"语言"某些类别,如英语,西班牙语,俄语等。

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

 <?php
    global $wpdb; // define global keyword

    $query = "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"; // Make query

    $result = $wpdb->get_results($query); //if Pass the variable in get_results function than get results

    print_r($result);

    ?>