我有两个职位类型(类型-A和类型-B)和两个分类(税-1和税-2),都分配给每种职位类型。这意味着来自A类的帖子可以包含来自税务-1和tax-2的术语,而来自B类的帖子也可以包含来自税务-1和tax-2的术语。
我希望我的WP_Query输出所有包含特定税收条款的A类文章。但我不想输出包含这些税务条款的B类帖子,不幸的是,我的WP_Query就是这样做的。这同样适用于税务-2,即只有来自B类的职位,其中包含来自税收-2的条件,应该输出。
我已经尝试为此创建两个$args,但是我没有设法合并两个$args。
function my_function($args) {
global $post;
$args = array(
'post_type' => array('type-A','type-B'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'tax-1',
'field' => 'term_id',
'terms' => array(11, 12, 13),
),
array(
'taxonomy' => 'tax-2',
'field' => 'term_id',
'terms' => array(21, 22, 23),
),
),
);
return $args;
} 发布于 2020-11-11 00:16:39
您可以使用预_到达_帖子根据post类型有条件地添加tax_query,下面是一个简单的示例。
get('post_type');
if ( 'type_a' === $post_type ) {
$tax_query = [
[
'taxonomy' => 'tax-1',
'field' => 'term_id',
'terms' => array(11, 12, 13),
]
];
}
elseif ( 'type_b' === $post_type ) {
$tax_query = [
[
'taxonomy' => 'tax-2',
'field' => 'term_id',
'terms' => array(21, 22, 23),
]
];
}
if ( !empty( $tax_query ) ) {
$the_query->set( 'tax_query', $tax_query );
}
}
add_action('pre_get_posts', 'wpse_377928');https://wordpress.stackexchange.com/questions/377928
复制相似问题