<p>You were on the right track using <code>relatedTo</code>. But as there's no <code>notRelatedTo</code> parameter, you'd first have to get all drinks that are related to any ingredient and those entries can then be removed from a new ElementCriteriaModel, that returns all your drinks, using Craft's <a href="http://buildwithcraft.com/docs/templating/filters#without" rel="nofollow noreferrer"><code>without</code></a> filter. Instead of passing whole element models, you should better use this filter with element IDs (→ <a href="https://craftcms.stackexchange.com/q/7170/125">Why is the Craft without filter not working correctly and how to debug?</a>). To get them just make use of the <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel#ids" rel="nofollow noreferrer"><code>ids()</code></a> method.</p>

<pre><code>{% set ingredients = craft.entries.section('ingredients').limit(null) %}
{% set drinksWithIngredientsIds = craft.entries.section('drinks').relatedTo({targetElement: ingredients}).limit(null).ids() %}

{% set drinksWithNoIngredients = craft.entries.section('drinks')|without(drinksWithIngredientsIds) %}
</code></pre>

CC BY-SA 3.0
2015-04-17T03:08:39.680Z
Genius! Thanks so much. On a minor note, I had to change the last line to retrieve ids: `{% set drinksWithNoIngredientsIds = craft.entries.section('drinks').ids()|without(drinksWithIngredientsIds) %}` for the `without` filter to kick in. I then was able to get all the entries I wanted via: `craft.entries.id(drinksWithNoIngredientsIds)`.
jx7
CC BY-SA 3.0
2015-04-17T03:10:28.043Z
Also, out of curiosity, what role does the `.limit(null)` parameter play in the first two lines of code?
jx7
CC BY-SA 3.0
2015-04-17T06:03:48.567Z
`limit` is set to 100 by default. So it just makes sure you include all the ingredients, last line is for the output and you can limit or paginate to your liking.
carlcs
CC BY-SA 3.0
2015-04-16T08:44:45.933Z
2015-04-16T08:50:31.923Z
2017-04-13T12:51:00.253Z
carlcs
<p>I'm new to all this. Is it possible to use <code>craft.entries</code> to return all entries with an empty entries field? Something like:</p>

<pre><code>{% set drinksWithNoIngredients = craft.entries.section("drinks").ingredients(":empty:") %}
{% for drink in drinksWithNoIngredients %}
    &lt;p&gt;Do something with {{ drink.title }}...&lt;/p&gt;
</code></pre>

<p>where <code>ingredients</code> is an entries field handle. This doesn't seem to work on entries fields, though. Or maybe I could do some kind of <code>relatedTo</code> magic, where the <code>targetElement</code> is some kind of special null value?</p>

<p>An alternative seems to be this:</p>

<pre><code>{% set drinksWithNoIngredients = craft.entries.section("drinks") %}
{% for drink in drinksWithNoIngredients %}
    {% if not drink.ingredients.total() %}
    &lt;p&gt;Do something with {{ drink.title }}...&lt;/p&gt;
</code></pre>

<p>But doing it that way calls <code>.total()</code> on <em>every</em> drink. Is that going to cause performance problems if I have a lot of drinks?</p>

<elementcriteriamodel>
Using craft.entries to return entries with empty entries field
# Using craft.entries to return entries with empty entries field

(Asked by: username_0 on 2015-04-16)

I'm new to all this. Is it possible to use craft.entries to return all entries with an empty entries field? Something like:
{% set drinksWithNoIngredients = craft.entries.section("drinks").ingredients(":empty:") %}
{% for drink in drinksWithNoIngredients %}
    Do something with {{ drink.title }}...

where ingredients is an entries field handle. This doesn't seem to work on entries fields, though. Or maybe I could do some kind of relatedTo magic, where the targetElement is some kind of special null value?
An alternative seems to be this:
{% set drinksWithNoIngredients = craft.entries.section("drinks") %}
{% for drink in drinksWithNoIngredients %}
    {% if not drink.ingredients.total() %}
    Do something with {{ drink.title }}...

But doing it that way calls .total() on every drink. Is that going to cause performance problems if I have a lot of drinks?

**Tags:** elementcriteriamodel

---

## 1 answers

### Answer by username_1 on 2015-04-16. Score: 2 | Accepted answer

You were on the right track using relatedTo. But as there's no notRelatedTo parameter, you'd first have to get all drinks that are related to any ingredient and those entries can then be removed from a new ElementCriteriaModel, that returns all your drinks, using Craft's without filter. Instead of passing whole element models, you should better use this filter with element IDs (→ Why is the Craft without filter not working correctly and how to debug?). To get them just make use of the ids() method.
{% set ingredients = craft.entries.section('ingredients').limit(null) %}
{% set drinksWithIngredientsIds = craft.entries.section('drinks').relatedTo({targetElement: ingredients}).limit(null).ids() %}

{% set drinksWithNoIngredients = craft.entries.section('drinks')|without(drinksWithIngredientsIds) %}

> **Comments:**
> **username_0**: Genius! Thanks so much. On a minor note, I had to change the last line to retrieve ids: `{% set drinksWithNoIngredientsIds = craft.entries.section('drinks').ids()|without(drinksWithIngredientsIds) %}` for the `without` filter to kick in. I then was able to get all the entries I wanted via: `craft.entries.id(drinksWithNoIngredientsIds)`.  
> **username_0**: Also, out of curiosity, what role does the `.limit(null)` parameter play in the first two lines of code?  
> **username_1**: `limit` is set to 100 by default. So it just makes sure you include all the ingredients, last line is for the output and you can limit or paginate to your liking.  

---

