这可以通过使用term\\u taxonomy\\u id而不是slug来完成,slug将有效地忽略指定的任何分类,只需查看唯一的term\\u taxonomy\\u id字段。这将使您能够有效地处理混合关系。您希望使用AND的整体关系,并使用in运算符将所有应该相关的术语或放在一个项目中。您需要首先将所需的术语映射到其term\\u taxonomy\\u ID。
$taxes = array( \'taxonomy1\', \'taxonomy2\', \'taxonomy3\', \'taxonomy4\' );
foreach ( $taxes as $tax ) {
$terms = get_terms( $tax );
foreach ( $terms as $term )
$tax_map[$tax][$term->slug] = $term->term_taxonomy_id;
}
$args[\'tax_query\'] => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'taxonomy1\',
\'field\' => \'term_taxonomy_id\',
\'terms\' => array( $tax_map[\'taxonomy1\'][$slug] )
\'operator\' => \'IN\',
),
array(
\'taxonomy\' => \'taxonomy3\',
\'field\' => \'term_taxonomy_id\',
\'terms\' => array( $tax_map[\'taxonomy3\'][$slug] ),
\'operator\' => \'IN\',
),
array(
\'taxonomy\' => \'taxonomy4\', // gets ignored
\'field\' => \'term_taxonomy_id\',
\'terms\' => array( $tax_map[\'taxonomy4\'][$slug], $tax_map[\'taxonomy2\'][$slug] ),
\'operator\' => \'IN\',
),
);
请注意,在3.5之前,您还需要指定
\'include_children\' => false
. 有关更多信息,请参阅此Trac记录单:
https://core.trac.wordpress.org/ticket/21228