因此,对于这类规则非常复杂的问题,我的解决方案是避免只为我的解决方案使用新的SQL,因为如果你必须向初学者解释它,它会很棘手。
我的解决方案:
function get_term_union( $taxonomy, $tax_query, $post_type = \'post\' ){
$args = array(
\'post_type\' => $post_type,
\'tax_query\' => $tax_query,
\'fields\' => \'ids\',
);
$posts = new WP_Query( $args );
$terms = array();
foreach( $posts as $post_id ){
$terms = array_merge( $terms, wp_get_post_terms( $post_id, $taxonomy, array( \'fields\' => \'ids\' ) ) );
}
$terms = array_unique( $terms );
return $terms;
}
// Usage
$tax_query = array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'taxonomy_B\',
\'field\' => \'slug\',
\'terms\' => \'term_A\',
),
array(
\'taxonomy\' => \'taxonomy_C\',
\'field\' => \'slug\',
\'terms\' => \'term_B\',
),
);
// This will return you all the term id\'s for `taxonomy_A`
var_dump( get_term_union( \'taxonomy_A\', $tax_query ) );
如果此解决方案不符合您的需要,请告诉我,我将尝试修复它。
向您致意,