Reputation: 484
How are WordPress wp_posts
and wp_postmeta
connected in MySQL? Thank you in advance..
Upvotes: 0
Views: 1589
Reputation: 873
Try this, Based on the link mentioned above, I was able to create this pattern to search custom posts_types based on 3 meta_values and 1 custom taxonomy.
So my structure is: Each custom post (talents) has 3 custom fields (City, Country, Gender). Each custom post belongs to the custom taxonomy "Languages" with certain categories such as English, Spanish, Russian etc.
So the query bellow is looking for a female in Canada, Toronto who can speak French.
<?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);
?>
Upvotes: 1
Reputation: 27092
The post_id
is an index in the wp_postmeta
table.
More info in the source.
Upvotes: 2