Answers.Body: <p>If the relation is (a) confined to a single field in <code>sectionA</code> and (b) that field can only relate to <code>sectionB</code> entries, you can just query for entries without a relation in that field using the <code>:empty:</code> parameter:</p>
  <pre class="lang-twig prettyprint-override"><code>{% set sectionAEntriesWithoutRelation = craft.entries()
      .section('sectionA')
      .related_section_b(':empty:')
      .all()
  %}
  </code></pre>
  <p>Replace <code>related_section_b</code> with the name of the Entries field.</p>
  <hr />
  <p>If you're looking for a solution that works for relations across multiple fields or for fields that may also relate to other sections, it's complicated. You can either use a patchy solution involving multiple queries – for example, get ALL sectionB entry IDs, get all sectionA entry IDs related to those and then exclude those from the final query. Or you can use a custom <code>.where()</code> condition – but that get's complicated fast, especially since element queries use a subquery to filter eligible entries and additional conditions are added to either the main query and/or the subquery in unintuitive ways. Look at the <code>relations</code> table to get started.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-06T00:19:03.153Z
Answers.Text: Thanks @MoritzLost I updated the question to give context about the setup. I need to query sectionA where a field in sectionB does not have any relation to sectionA. I am guessing it maybe a where clause which may need to be done. I didn't want to grab ID and compare then as there are 1000's of them, it feels it would be more performant to be able to query sectionA where no entry is related to a specific field in sectionB
Answers.UserDisplayName: zizther
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-08T10:11:39.980Z
Answers.Text: @zizther Ok I see, the `:empty:` selector won't help if the relationship field is not set on the entries you're querying for. Looks like natively there's no way around a custom query then. Or maybe you could check out the [Reverse Relations](https://plugins.craftcms.com/reverserelations) plugin – I haven't tried it, but it's supposed to save relations in both directions, that could make querying much easier.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-09T13:19:00.403Z
Answers.Text: Thanks I will take a look. Would be nice to find a way where it wouldn't have to do additional lookups and comparisons.
Answers.UserDisplayName: zizther
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-05T13:49:07.863Z
Answers.LastActivityDate: 2021-11-05T13:49:07.863Z
Answers.UserDisplayName: MoritzLost
Body: <p>My scenario is I have entries in <code>sectionA</code> these are related to entries in <code>sectionB</code>.
  I want to be able to show all entries from <code>sectionA</code> which are not related to any entry in <code>sectionB</code>.</p>
  <p><code>SectionB</code> has an Entries field which only relates entries from <code>SectionA</code>. I need to query <code>sectionA</code> which are not related from <code>sectionB</code>.</p>
  <p>Is there a nice way to get these entries without having to compare IDs arrays or loop through to check each entry?</p>
  
Tags: <entries><relatedto>
Title: Get entries which are not related in to another section
ThreadText: # Get entries which are not related in to another section
  
  (Asked by: username_0 on 2021-11-05)
  
  My scenario is I have entries in `sectionA` these are related to entries in `sectionB`.
  I want to be able to show all entries from `sectionA` which are not related to any entry in `sectionB`.
  
  `SectionB` has an Entries field which only relates entries from `SectionA`. I need to query `sectionA` which are not related from `sectionB`.
  
  Is there a nice way to get these entries without having to compare IDs arrays or loop through to check each entry?
  
  **Tags:** entries, relatedto
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-11-05. Score: 1
  
  If the relation is (a) confined to a single field in `sectionA` and (b) that field can only relate to `sectionB` entries, you can just query for entries without a relation in that field using the `:empty:` parameter:
  
  ```twig
  {% set sectionAEntriesWithoutRelation = craft.entries()
      .section('sectionA')
      .related_section_b(':empty:')
      .all()
  %}
  
  ```
  
  Replace `related_section_b` with the name of the Entries field.
  
  ---
  
  If you're looking for a solution that works for relations across multiple fields or for fields that may also relate to other sections, it's complicated. You can either use a patchy solution involving multiple queries – for example, get ALL sectionB entry IDs, get all sectionA entry IDs related to those and then exclude those from the final query. Or you can use a custom `.where()` condition – but that get's complicated fast, especially since element queries use a subquery to filter eligible entries and additional conditions are added to either the main query and/or the subquery in unintuitive ways. Look at the `relations` table to get started.
  
  > **Comments:**
  > **username_0**: Thanks @username_1 I updated the question to give context about the setup. I need to query sectionA where a field in sectionB does not have any relation to sectionA. I am guessing it maybe a where clause which may need to be done. I didn't want to grab ID and compare then as there are 1000's of them, it feels it would be more performant to be able to query sectionA where no entry is related to a specific field in sectionB  
  > **username_1**: @username_0 Ok I see, the `:empty:` selector won't help if the relationship field is not set on the entries you're querying for. Looks like natively there's no way around a custom query then. Or maybe you could check out the [Reverse Relations](https://plugins.craftcms.com/reverserelations) plugin – I haven't tried it, but it's supposed to save relations in both directions, that could make querying much easier.  
  > **username_0**: Thanks I will take a look. Would be nice to find a way where it wouldn't have to do additional lookups and comparisons.  
  
  ---
  
  