如何用逗号分隔分类术语,以便它可以动态包含在税务查询中
how to comma separate the taxonomy terms so it could be included dynamically in the tax query
我已经设法 return 为 post 选择的分类法,但是,这些被 return 编辑为一个附加列表,例如 covid1covid2covid3
。我希望能够将它们分开 covid1, covid2, covid3
(而不是 return 最后一个逗号),以便我可以在我的税务查询中使用它 return 动态相关 post基于这些分类法。请参阅下面的代码
$currentID = get_the_ID();
$post_terms = get_the_terms($currentID, 'vocabulary_1');
foreach ($post_terms as $post_term) {
$postTerm = $post_term->slug;
}
$args1 = array(
'post_type' => 'cme-education',
'posts_per_page' => '1',
'post_status' => 'publish',
'orderby'=>'rand',
'tax_query' => array(
array(
'taxonomy' => 'vocabulary_1',
'field' => 'slug',
'terms' => array($postTerm),
'operator' => 'IN',
'order' => 'ASC'
)
)
);
问题在 $postTerm
。首先将其设为数组:
$postTerm = [];
然后将术语存储在其中,如下所示:
foreach ($post_terms as $post_term) {
$postTerm[] = $post_term->slug;
}
完整代码如下:
$currentID = get_the_ID();
$post_terms = get_the_terms($currentID, 'vocabulary_1');
$postTerm = [];
foreach ($post_terms as $post_term) {
$postTerm[] = $post_term->slug;
}
$args1 = array(
'post_type' => 'cme-education',
'posts_per_page' => '1',
'post_status' => 'publish',
'orderby' =>'rand',
'tax_query' => array(
array(
'taxonomy' => 'vocabulary_1',
'field' => 'slug',
'terms' => $postTerm,
'operator' => 'IN',
'order' => 'ASC'
)
)
);
我已经设法 return 为 post 选择的分类法,但是,这些被 return 编辑为一个附加列表,例如 covid1covid2covid3
。我希望能够将它们分开 covid1, covid2, covid3
(而不是 return 最后一个逗号),以便我可以在我的税务查询中使用它 return 动态相关 post基于这些分类法。请参阅下面的代码
$currentID = get_the_ID();
$post_terms = get_the_terms($currentID, 'vocabulary_1');
foreach ($post_terms as $post_term) {
$postTerm = $post_term->slug;
}
$args1 = array(
'post_type' => 'cme-education',
'posts_per_page' => '1',
'post_status' => 'publish',
'orderby'=>'rand',
'tax_query' => array(
array(
'taxonomy' => 'vocabulary_1',
'field' => 'slug',
'terms' => array($postTerm),
'operator' => 'IN',
'order' => 'ASC'
)
)
);
问题在 $postTerm
。首先将其设为数组:
$postTerm = [];
然后将术语存储在其中,如下所示:
foreach ($post_terms as $post_term) {
$postTerm[] = $post_term->slug;
}
完整代码如下:
$currentID = get_the_ID();
$post_terms = get_the_terms($currentID, 'vocabulary_1');
$postTerm = [];
foreach ($post_terms as $post_term) {
$postTerm[] = $post_term->slug;
}
$args1 = array(
'post_type' => 'cme-education',
'posts_per_page' => '1',
'post_status' => 'publish',
'orderby' =>'rand',
'tax_query' => array(
array(
'taxonomy' => 'vocabulary_1',
'field' => 'slug',
'terms' => $postTerm,
'operator' => 'IN',
'order' => 'ASC'
)
)
);