<p>I know the long-form method for achieving my goal, but I'm hitting a wall in trying to keep the conditionals down.</p>

<p>Is it possible to search for entries related to a specific category field <strong>AND</strong> search for entries with no category selection for that same field?</p>

<p>Right now, I can only do the first part. Here's a snippet of a much larger query.</p>

<pre><code>{# get all categories within specific category groups #}
{% set skillsOptions = craft.categories.group('skillsCategories') %}
{% set durationOptions = craft.categories.group('durationCategories') %}

{# working: find all entries with any category selected #}
{# not working: also find all entries with no category selected #}
{% set opportunities = craft.entries.section('opportunities').relatedTo('and',
    {
        targetElement: ['or', skillsOptions],
        field: 'skills'
    },
    {
        targetElement: ['or', durationOptions],
        field: 'duration'
    }
%}
</code></pre>

CC BY-SA 3.0
2017-08-18T04:11:08.747Z
Similar question I didn't see before: https://craftcms.stackexchange.com/questions/12391/get-entries-where-relation-matches-or-is-empty
Stephen Callender
CC BY-SA 3.0
2017-08-18T04:12:03.983Z
I've decided to require the category fields and added an "undefined" option that won't show in our filters. That does the trick.
Stephen Callender
<categories><relatedto>
Advanced search entries relatedTo category and unrelated to any category
# Advanced search entries relatedTo category and unrelated to any category

(Asked by: username_0 on 2017-08-17)

I know the long-form method for achieving my goal, but I'm hitting a wall in trying to keep the conditionals down.
Is it possible to search for entries related to a specific category field AND search for entries with no category selection for that same field?
Right now, I can only do the first part. Here's a snippet of a much larger query.
{# get all categories within specific category groups #}
{% set skillsOptions = craft.categories.group('skillsCategories') %}
{% set durationOptions = craft.categories.group('durationCategories') %}

{# working: find all entries with any category selected #}
{# not working: also find all entries with no category selected #}
{% set opportunities = craft.entries.section('opportunities').relatedTo('and',
    {
        targetElement: ['or', skillsOptions],
        field: 'skills'
    },
    {
        targetElement: ['or', durationOptions],
        field: 'duration'
    }
%}

**Tags:** categories, relatedto

> **Comments:**
> **username_0**: Similar question I didn't see before: https://craftcms.stackexchange.com/questions/12391/get-entries-where-relation-matches-or-is-empty  
> **username_0**: I've decided to require the category fields and added an "undefined" option that won't show in our filters. That does the trick.  

---

## 0 answers

