如何用逗号分隔分类术语,以便将其动态包含在税务查询中

发布于 2025-01-17 20:30:07 字数 795 浏览 0 评论 0原文

我已经设法返回已为帖子选择的分类法,但是,这些分类法将作为一个附录列表返回,例如covid1covid2covid3。我希望能够将这些covid1,covid2,covid3(不返回最后一个逗号)将其分开,以便我可以在税务查询中使用它,该查询将根据这些分类法动态返回相关的帖子。在下面查看我的代码

  $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'
        )
    )
);

I have managed to return the taxonomies that have been selected for a post however, these are returned as one appended list eg covid1covid2covid3. I want to be able to separate these out covid1, covid2, covid3 (not returning the last comma) so that I can use it in my tax query which would return related post dynamically based on these taxonomies. See my code below

  $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'
        )
    )
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2025-01-24 20:30:07

问题出在 $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'
        )
    )
);

The issue is in $postTerm. First make it array:

$postTerm = [];

then store terms in it, like this:

foreach ($post_terms as $post_term) {
    $postTerm[] = $post_term->slug;
}

Here is the complete code:

$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'
        )
    )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文