如果条款2
, 32
, 71
和38
属于相同的分类法,如language
, 然后看看我原来的答案。
否则WP_Query
希望如此:
$query = new WP_Query( [
\'post_type\' => \'property\',
\'tax_query\' => [
\'relation\' => \'AND\', // for clauses 1 and 2
[ // clause 1
\'taxonomy\' => \'language\',
\'terms\' => 2,
],
[ // clause 2
\'relation\' => \'OR\', // for sub-clauses 1, 2 and 3
[ // sub-clause 1
\'taxonomy\' => \'taxonomy-name\',
\'terms\' => 32,
],
[ // sub-clause 2
\'taxonomy\' => \'taxonomy-name-2\',
\'terms\' => 71,
],
[ // sub-clause 3
\'taxonomy\' => \'taxonomy-name-3\',
\'terms\' => 38,
],
],
],
] );
这应该给你正确的
IN() AND ( IN() OR IN() OR IN() )
查询,对于SQL命令,只需执行以下操作
echo $query->request;
然后使用命令执行任何需要的操作。:)
并确保用正确的名称替换分类名称。
查询分配给术语的帖子2
然后是分配给术语的32
, 71
, 38
可以在WP_Query
通过tax_query
参数,如下所示:
$query = new WP_Query( [
\'post_type\' => \'property\',
\'tax_query\' => [
\'relation\' => \'AND\', // AND is the default
[
\'taxonomy\' => \'language\',
\'terms\' => 2,
],
[
\'taxonomy\' => \'language\',
\'terms\' => [ 32, 71, 38 ],
],
],
] );
如果您从上面的查询中检查SQL命令,即。
echo $query->request
, 该命令有两个
LEFT JOIN
条款:
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)
以及
WHERE
对于
tax_query
看起来像:
wp_term_relationships.term_taxonomy_id IN (2)
AND
tt1.term_taxonomy_id IN (32,71,38)
因此,您的SQL命令应该是这样的-即使用两个(左/内/等)连接与第一个连接来查询术语中的帖子
2
, 第二个是条款中的职位
32
,
71
和/或
38
.
下面是一个基于SQL命令的示例,除了(如上所述),我使用了前缀wp_
而不是agn_
, 我还使用了表别名,如p
对于wp_posts
:
(注意,在这个示例中,我故意不使用SQL_CALC_FOUND_ROWS
)
SELECT p.ID, p.post_title, tr.term_taxonomy_id
FROM wp_posts p
LEFT JOIN wp_term_relationships tr ON p.ID = tr.object_id
LEFT JOIN wp_term_relationships tr2 ON p.ID = tr2.object_id
WHERE 1 = 1
AND tr.term_taxonomy_id IN(2)
AND tr2.term_taxonomy_id IN(32, 71, 38)
AND p.post_type = \'property\'
AND (p.post_status = \'publish\' OR p.post_status = \'private\')
GROUP BY p.ID
ORDER BY RAND() # note that this can get VERY slow on large data/tables
LIMIT 0, 50