如何获取所有具有分类术语和特定名称/段塞的帖子?
我尝试了以下论点,但没有得到想要的结果。我缺少什么,如何在同一个查询中组合slug和term?
array(
\'name\' => \'<name of post>\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'<taxonomy>\',
\'field\' => \'slug\',
\'terms\' => <slug>,
\'operator\' => \'IN\'
)
),
)
SQL:
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = \'<slug-name>\' AND wp_posts.post_type = \'<post type>\' AND ((wp_posts.post_status = \'publish\')) ORDER BY wp_posts.post_date DESC
如果删除“name”字段,则SQL的分类法部分看起来是正确的。
array(
\'tax_query\' => array(
array(
\'taxonomy\' => \'<taxonomy>\',
\'field\' => \'slug\',
\'terms\' => <slug>,
\'operator\' => \'IN\'
)
),
)
SQL:
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND (
wp_term_relationships.term_taxonomy_id IN (2)
) AND wp_posts.post_type = \'<post type>\' AND ((wp_posts.post_status = \'publish\')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 5"
最合适的回答,由SO网友:birgire 整理而成
注意,如果您有post slug,则不需要额外的分类查询来查找该post,因此在这种情况下,在core中删除昂贵的分类查询是有意义的。
否则,您可以尝试post_name__in
参数:
[
\'post_name__in\' => [ \'<postslug>\' ],
\'tax_query\' => [
[
\'taxonomy\' => \'<taxonomy>\',
\'field\' => \'slug\',
\'terms\' => <slug>,
\'operator\' => \'IN\'
]
],
];
这是WordPress 4.4中引入的,如果您真的希望还包括分类查询部分;-)