最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How To Query For An Empty Taxonomy Field

programmeradmin3浏览0评论

I want to write a query that looks up taxonomies. One condition is that taxonomyA returns x, and a second condition is that taxonomyB returns empty. I don't know how to query for empty.

This is what I have:

$args = [
    'post_type' => 'post',
    'tax_query' => [
        'relation' => 'AND',
        [
            'taxonomy'         => 'areaoflondon',
            'field'            => 'name',
            'terms'            => 'South London',
            'include_children' => false,
        ],
        [
            'taxonomy'         => 'yearofvisit',
            'field'            => 'name',
            'terms'            => '',
            'include_children' => false,
          ]
    ],
    'posts_per_page'     => 5,
    'meta_key' => 'rating',
    'meta_type' => 'NUMERIC',
    'order' => 'DESC',
    'orderby' => 'meta_value_num',
];

However this doesn't return any results.

How can I query that a taxonomy is empty? Is it even possible?

Thanks James

I want to write a query that looks up taxonomies. One condition is that taxonomyA returns x, and a second condition is that taxonomyB returns empty. I don't know how to query for empty.

This is what I have:

$args = [
    'post_type' => 'post',
    'tax_query' => [
        'relation' => 'AND',
        [
            'taxonomy'         => 'areaoflondon',
            'field'            => 'name',
            'terms'            => 'South London',
            'include_children' => false,
        ],
        [
            'taxonomy'         => 'yearofvisit',
            'field'            => 'name',
            'terms'            => '',
            'include_children' => false,
          ]
    ],
    'posts_per_page'     => 5,
    'meta_key' => 'rating',
    'meta_type' => 'NUMERIC',
    'order' => 'DESC',
    'orderby' => 'meta_value_num',
];

However this doesn't return any results.

How can I query that a taxonomy is empty? Is it even possible?

Thanks James

Share Improve this question asked Feb 20, 2022 at 9:57 iwillbeawebdeveloperiwillbeawebdeveloper 1212 silver badges8 bronze badges 5
  • Have you tried using the EXISTS operator? E.g. [ 'taxonomy' => 'yearofvisit', 'operator' => 'EXISTS' ] – Sally CJ Commented Feb 20, 2022 at 12:01
  • Or maybe try the NOT EXISTS operator, depending on what the "empty taxonomy" means in your code/context. – Sally CJ Commented Feb 20, 2022 at 12:11
  • That is perfect - much appreciated. – iwillbeawebdeveloper Commented Feb 20, 2022 at 18:37
发布评论

评论列表(0)

  1. 暂无评论