wordpresstax_query,其中分类法为空

标签 wordpress taxonomy

我使用多个分类查询来获取相关帖子

    $tax_query[] = array(
        'taxonomy' => 'transfer_type',
        'field'    => 'id',
        'terms'     => $page_terms['type_term'],
        'include_children' => false
    );


    $tax_query[] = array(
        'taxonomy' => 'area',
        'field'    => 'id',
        'terms'     => $page_terms['area_term'],
        'include_children' => false
    );

    $args = array(
        'post_type' => 'category_description',
        'tax_query' => $tax_query
    );

$description_post = get_posts($args);

当帖子被标记为transfer_type和区域时没有问题, 但是,当一篇文章仅用其中一个标记时,结果是错误的。

我基本上(在某些情况下)想要排除所有具有“区域”或“transfer_type”的帖子,而只获取那些与其他帖子相遇的帖子。

这可能吗?

最佳答案

弄清楚了......(不知道这是否是最好的,但它仍然是一个解决方案)

如果其中一个分类为空,我会对整个分类术语使用“NOT IN”运算符

        $terms = get_terms("transfer_type");
        foreach($terms as $term){
            $not_in_type[] = $term->term_id; 
        }

        $terms = get_terms("area");
        foreach($terms as $term){
            $not_in_area[] = $term->term_id; 
        }


        $tax_query[] = array(
            'taxonomy'         => 'transfer_type',
            'field'            => 'id',
            'terms'            => $page_terms['type_term'] ? $page_terms['type_term'] : $not_in_type,
            'include_children' => false,
            'operator'         => $page_terms['type_term'] ? 'IN' : 'NOT IN'
        );

        $tax_query[] = array(
            'taxonomy'         => 'area',
            'field'            => 'id',
            'terms'            => $page_terms['area_term'] ? $page_terms['area_term'] : $not_in_area,
            'include_children' => false,
            'operator'         => $page_terms['area_term'] ? 'IN' : 'NOT IN'
        );

关于wordpresstax_query,其中分类法为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17270615/

相关文章:

Mysql 搜索并替换为随机字符

wordpress - 如何使 woocommerce 产品标签分层?

html - 从 Sharepoint 服务器更改客户端分类选择器的样式

php - 创建分类法时删除描述框

css - 如何让我的 flexbox 按钮悬停?

php - 如何根据变量的值对列表进行排序? (php)

wordpress - 将用户帐户商务服务器集成到 wordpress

wordpress - 不要在页面加载时从 WordPress 中删除空的 p (<p> </p>) 标签

orchardcms - Orchard CMS 分类、导入术语

ruby-on-rails - Spree 仅在不同页面的侧边栏上显示选定的分类单元