我知道这是一个老帖子,但我只是想插嘴@jdm2112正确。只需使用WP\\u查询类。这可以确保如果将来数据库结构更新,您的查询仍然有效。。。。然而,由于看起来您正在寻找原始SQL答案,因此这是实际的SQLWP_Query
将执行。。。至少从WordPress 5.6开始。(我已经根据您的问题调整了SQL)
SELECT * from wp_posts
LEFT JOIN
wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE
(wp_term_relationships.term_taxonomy_id IN (37)) AND
wp_posts.post_type = \'recipe_cpt\' AND
wp_posts.post_status = \'publish\'
GROUP BY wp_posts.ID
您可以通过创建
WP_Query
实例,然后查看
request
类似这样的属性:
<?php
$args = [
\'post_type\' => \'recipe_cpt\',
\'tax_query\' => [
[
\'taxonomy\' => \'recipe_tx\',
\'field\' => \'term_id\',
\'terms\' => 37
]
]
];
$queryObj = new WP_Query($args);
// This will give you the actual raw SQL query
$rawSqlQuery = $queryObj->request;