<p>I think I have a solution: if I create an array and add the entry id's for the <code>databytes</code> that are being used in the <code>databyteSeries</code> collections, then I can check that the entries are not in the array and display all the remaining entries from the <code>databytes</code> and <code>databyteSeries</code> channels. Here is a simple test that I am working with:</p>

<pre><code>{% set alreadyUsedDatabytes = [] %}

{% set databyteSeries = craft.entries.section('databyteSeries') %}
{% for entry in craft.entries.section('databytes').relatedTo(databyteSeries) %}
  {% set alreadyUsedDatabytes = alreadyUsedDatabytes|merge([entry.id]) %}
{% endfor %}
&lt;ul&gt;
  {% for entry in craft.entries.section(['databytes','databyteSeries']) %}
  {% if entry.id not in alreadyUsedDatabytes %}
  &lt;li&gt;
    &lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;
  &lt;/li&gt;
  {% endif %}
  {% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2014-07-05T20:31:44.380Z
2014-07-05T20:31:44.380Z
Gareth
<p>Your question is just about entries without relationships but your code example is attempting to display both items with a relationship and without a relationship.  I think your example is making things a bit more complicated than this needs to be.  Rather than looping through both sections (databytes and databyteSeries), I believe you only need to loop through one section.  The <code>databyteSeries</code> section, as you've described it, has a relation field for <code>databytes</code>, so all the info you need to know about <code>databytes</code> should be available to you in the relation field.</p>

<p>Let me know if the following example gets at what you are going for.  It simplifies your loop to just the <code>databyteSeries</code> section, and then uses Twig's length filter to test the <code>entry.relatedDatabytes</code> field to see if it has any entries. If there are related databytes, it displays one output, and if there are not, it displays an alternative output.</p>

<p>I've also updated the method in which you were using to output the first item in the series.  Rather than loop through the entire <code>entry.relatedDatabytes</code> field and just output the first item using a <code>loop.first</code> conditional, I've updated the code to use <code>entry.relatedDatabytes.first()</code>.  Using the <code>first()</code> method on an EntryModel will just grab the first item, and then you can output it as you wish with whatever variable you assign it to.  Below, I've assigned the first item to the <code>firstInSeries</code> variable.</p>

<pre><code>{% paginate craft.entries
     .section('databyteSeries')
     .limit(6)
     as entries 
%}

    {# Loop through databyteSeries entries #}
    {% for entry in entries %}

        {% if entry.relatedDatabytes|length %}

            {# Display the first item in a Databyte Series that has a relationship #}
            {% set firstInSeries = entry.relatedDatabytes.first() %}
        {{ firstInSeries.title }}

      {% else %}

        {# Display the Databyte Series name that has no relationships #}
        {{ entry.title }}

      {% endif %}

    {% endfor %}

{% endpaginate %}
</code></pre>

<p><strong>UPDATE</strong></p>

<p>Based on your clarification in the comments and your potential solution, here's another method to consider using Craft's <a href="http://buildwithcraft.com/docs/templating/filters#without" rel="nofollow">without</a> filter:</p>

<pre><code>{# Get all databyteSeries and databytes #}
{% set databyteSeries = craft.entries.section('databyteSeries').limit(null) %}
{% set databytes = craft.entries.section('databytes').limit(null).indexBy('id') %}

{# Determine which databytes already have a relationship to a databyteSeries #}
{% set alreadyUsedDatabytes = craft.entries.section('databytes').relatedTo(databyteSeries).indexBy('id') %}

{# Use the without filter to remove all 'alreadyUsedDatabytes' from the complete 'databytes' result, so you are left with only 'unusedDatabytes'  #}
{% set unusedDatabytes = databytes|without(alreadyUsedDatabytes) %}

{# Output the unusedDatabytes #}
&lt;ul&gt;
{% for entry in unusedDatabytes %}
  &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
</code></pre>

<p>I have not tested this code so let me know if you run into any issues to troubleshoot.</p>

CC BY-SA 3.0
2014-07-05T19:27:57.497Z
Hi Ben, thank you for your answer. I am probably not explaining myself very well so apologies. What I have is a channel called `databytes` which holds all the entries. The channel `databyteSeries` is just for selecting certain `databytes` to display as a "collection". If a databyte has been selected in a `databyteSeries` then I want to display the first one in that "collection" on the page. What I then need to do is display all other databytes that are not part of a collection. You can see an example here: http://graph.domaintesting.me/databytes hopefully that makes sense.
Gareth
CC BY-SA 3.0
2014-07-05T20:24:36.577Z
Do these two queries need to happen in the same page?  Or can we break this up into two problems 1) Display the first databyte entry in each databyteSeries, 2) Display all databytes that do not belong to a databyteSeries ?
Ben Parizek
CC BY-SA 3.0
2014-07-05T20:33:20.317Z
Hi Ben, yes these two queries need to run on the same page I have added my attempt at a solution below. It seems to work in a simple test I have run, just need to add it to the main site template now.
Gareth
CC BY-SA 3.0
2014-07-05T21:03:51.087Z
Hey Gareth, based on your clarification I've updated my answer above. Please consider the possible solution outlined in the 'UPDATE' section.  If that works and is more on target, I can clean up the rest of my answer and get rid of the code around my initial misunderstanding.
Ben Parizek
CC BY-SA 3.0
2014-07-06T06:52:55.410Z
Hi Ben, the updated code looks like its just what I am after only for some reason its outputting all the databytes. I have tested the `alreadyUsedDatabytes` variable and that is working but for some reason `{% set unusedDatabytes = databytes|without(alreadyUsedDatabytes) %}` still outputs all the databytes with non of the `alreadyUsedDatabytes` removed.
Gareth
CC BY-SA 3.0
2014-07-06T15:46:52.370Z
Ok, one more try, I just added .indexBy('id') to the `databytes` and `alreadyUsedDatabytes` queries. I'm hoping that helps the `without` filter recognize what needs to match up.
Ben Parizek
CC BY-SA 3.0
2014-07-06T18:17:11.097Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/15565/discussion-between-gareth-redfern-and-ben-parizek).
Gareth
CC BY-SA 3.0
2014-07-04T23:39:12.013Z
2014-07-06T15:45:30.237Z
2014-07-06T15:45:30.237Z
Ben Parizek
<p>I think you should consider using a structure for your entries. Try a structure with Max Levels = 2 and two field types: <em>single</em> and <em>series</em>. Organize the structure like this:</p>

<ul>
<li>single</li>
<li>single</li>
<li>series
<ul>
<li>single</li>
<li>single</li>
</ul></li>
<li>etc.</li>
</ul>

<p>Then to loop over the entries:</p>

<pre><code>{% for dataByte in craft.entries.section('dataByte').level(1) %}
  {% switch dataByte.type %}
  {% case 'single' %}
      {# TEMPLATE CODE FOR STAND ALONE ENTRY HERE #}
      single: {{dataByte.title}}&lt;br/&gt;
  {% case 'series' %}
    {% set firstInSeries = dataByte.getChildren().first() %}
    {% if firstInSeries %}
      {# TEMPLATE CODE FOR FIRST ENTRY IN SERIES HERE #}
      seriesFirst: {{firstInSeries.title}}&lt;br/&gt;
    {% endif %}
  {% endswitch %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-07-07T11:47:00.067Z
Thank you Marion, I hadn't looked at this for an option. I have now come up with a solution that is a mixture of my answer and Ben's but I appreciate your help and will definitely consider this in the future.
Gareth
CC BY-SA 3.0
2014-07-06T22:18:24.073Z
2014-07-06T22:18:24.073Z
Marion Newlevant
<p>I have two channels set up, the first creates entries called "databytes" the second is a channel for relating certain databytes to each other called "databyteSeries". In my template I want to output all databytes but if they are related just show the first in the series. I have the following code which is displaying the first in the databyteSeries no problem (sectionId 8):</p>

<pre><code>{% paginate craft.entries.section(['databytes','databyteSeries']).limit(6) as entries %}

{% for entry in entries %}

  {% if entry.sectionId == 8 %}

  {% for series in entry.relatedDatabytes %}
    {% if loop.first %}
      {# TEMPLATE CODE FOR ENTRY HERE #}
    {% endif %}
  {% endfor %}

  {% else %}

    {# THIS NEEDS TO DISPLAY ALL DATABYTES THAT ARE NOT RELATED #}

  {% endif %}

{% endfor %}

{# PAGINATION CODE HERE #}

{% endpaginate %}
</code></pre>

<p>It's the second part where I now only want to display the databytes that have NOT been related where I am struggling. I guess it needs some kind of reverse relationship where I have put the <code>{# THIS NEEDS TO DISPLAY ALL DATABYTES THAT ARE NOT RELATED #}</code> but I am not sure.</p>

<p>Any help would be appreciated.</p>

CC BY-SA 3.0
2014-07-04T09:17:43.503Z
You make it really hard for anyone willing to help you. Could you please slim down your code and leave out irrelevant parts (pagination links, loops for your assets, html only parts)?
carlcs
CC BY-SA 3.0
2014-07-04T09:28:13.103Z
Sorry, does the new code help and make sense?
Gareth
CC BY-SA 3.0
2014-07-04T09:54:10.883Z
Thanks Gareth. I suppose you probably use an [Entries field](http://buildwithcraft.com/docs/entries-fields) to make up your relations. Is there a reason that I don't get yet why you don't use the [relateTo](http://buildwithcraft.com/docs/relations#the-relatedTo-param) parameter to query the entries?
carlcs
CC BY-SA 3.0
2014-07-04T10:05:09.147Z
Hi Christian, yes I have used the Entries field to make the relations and to be honest I am just trying to work my way through a possible solution, it may well be I have missed something really simple and obvious. All I need to do is output all entries from both channels but when an entry is part of a series (which is the `databyteSeries` channel) just display the first entry in that series and don't display it from the `databytes` channel.
Gareth
<relations>
Displaying Entries That Are Not Related
# Displaying Entries That Are Not Related

(Asked by: username_0 on 2014-07-04)

I have two channels set up, the first creates entries called "databytes" the second is a channel for relating certain databytes to each other called "databyteSeries". In my template I want to output all databytes but if they are related just show the first in the series. I have the following code which is displaying the first in the databyteSeries no problem (sectionId 8):
{% paginate craft.entries.section(['databytes','databyteSeries']).limit(6) as entries %}

{% for entry in entries %}

  {% if entry.sectionId == 8 %}

  {% for series in entry.relatedDatabytes %}
    {% if loop.first %}
      {# TEMPLATE CODE FOR ENTRY HERE #}
    {% endif %}
  {% endfor %}

  {% else %}

    {# THIS NEEDS TO DISPLAY ALL DATABYTES THAT ARE NOT RELATED #}

  {% endif %}

{% endfor %}

{# PAGINATION CODE HERE #}

{% endpaginate %}

It's the second part where I now only want to display the databytes that have NOT been related where I am struggling. I guess it needs some kind of reverse relationship where I have put the {# THIS NEEDS TO DISPLAY ALL DATABYTES THAT ARE NOT RELATED #} but I am not sure.
Any help would be appreciated.

**Tags:** relations

> **Comments:**
> **username_1**: You make it really hard for anyone willing to help you. Could you please slim down your code and leave out irrelevant parts (pagination links, loops for your assets, html only parts)?  
> **username_0**: Sorry, does the new code help and make sense?  
> **username_1**: Thanks username_0. I suppose you probably use an [Entries field](http://buildwithcraft.com/docs/entries-fields) to make up your relations. Is there a reason that I don't get yet why you don't use the [relateTo](http://buildwithcraft.com/docs/relations#the-relatedTo-param) parameter to query the entries?  
> **username_0**: Hi Christian, yes I have used the Entries field to make the relations and to be honest I am just trying to work my way through a possible solution, it may well be I have missed something really simple and obvious. All I need to do is output all entries from both channels but when an entry is part of a series (which is the `databyteSeries` channel) just display the first entry in that series and don't display it from the `databytes` channel.  

---

## 3 answers

### Answer by username_0 on 2014-07-05. Score: 1 | Accepted answer

I think I have a solution: if I create an array and add the entry id's for the databytes that are being used in the databyteSeries collections, then I can check that the entries are not in the array and display all the remaining entries from the databytes and databyteSeries channels. Here is a simple test that I am working with:
{% set alreadyUsedDatabytes = [] %}

{% set databyteSeries = craft.entries.section('databyteSeries') %}
{% for entry in craft.entries.section('databytes').relatedTo(databyteSeries) %}
  {% set alreadyUsedDatabytes = alreadyUsedDatabytes|merge([entry.id]) %}
{% endfor %}

  {% for entry in craft.entries.section(['databytes','databyteSeries']) %}
  {% if entry.id not in alreadyUsedDatabytes %}
  
    {{ entry.title }}
  
  {% endif %}
  {% endfor %}


---

### Answer by username_2 on 2014-07-04. Score: 3

Your question is just about entries without relationships but your code example is attempting to display both items with a relationship and without a relationship.  I think your example is making things a bit more complicated than this needs to be.  Rather than looping through both sections (databytes and databyteSeries), I believe you only need to loop through one section.  The databyteSeries section, as you've described it, has a relation field for databytes, so all the info you need to know about databytes should be available to you in the relation field.
Let me know if the following example gets at what you are going for.  It simplifies your loop to just the databyteSeries section, and then uses Twig's length filter to test the entry.relatedDatabytes field to see if it has any entries. If there are related databytes, it displays one output, and if there are not, it displays an alternative output.
I've also updated the method in which you were using to output the first item in the series.  Rather than loop through the entire entry.relatedDatabytes field and just output the first item using a loop.first conditional, I've updated the code to use entry.relatedDatabytes.first().  Using the first() method on an EntryModel will just grab the first item, and then you can output it as you wish with whatever variable you assign it to.  Below, I've assigned the first item to the firstInSeries variable.
{% paginate craft.entries
     .section('databyteSeries')
     .limit(6)
     as entries 
%}

    {# Loop through databyteSeries entries #}
    {% for entry in entries %}

        {% if entry.relatedDatabytes|length %}

            {# Display the first item in a Databyte Series that has a relationship #}
            {% set firstInSeries = entry.relatedDatabytes.first() %}
        {{ firstInSeries.title }}

      {% else %}

        {# Display the Databyte Series name that has no relationships #}
        {{ entry.title }}

      {% endif %}

    {% endfor %}

{% endpaginate %}

UPDATE
Based on your clarification in the comments and your potential solution, here's another method to consider using Craft's without filter:
{# Get all databyteSeries and databytes #}
{% set databyteSeries = craft.entries.section('databyteSeries').limit(null) %}
{% set databytes = craft.entries.section('databytes').limit(null).indexBy('id') %}

{# Determine which databytes already have a relationship to a databyteSeries #}
{% set alreadyUsedDatabytes = craft.entries.section('databytes').relatedTo(databyteSeries).indexBy('id') %}

{# Use the without filter to remove all 'alreadyUsedDatabytes' from the complete 'databytes' result, so you are left with only 'unusedDatabytes'  #}
{% set unusedDatabytes = databytes|without(alreadyUsedDatabytes) %}

{# Output the unusedDatabytes #}

{% for entry in unusedDatabytes %}
  {{ entry.title }}
{% endfor %}


I have not tested this code so let me know if you run into any issues to troubleshoot.

> **Comments:**
> **username_0**: Hi Ben, thank you for your answer. I am probably not explaining myself very well so apologies. What I have is a channel called `databytes` which holds all the entries. The channel `databyteSeries` is just for selecting certain `databytes` to display as a "collection". If a databyte has been selected in a `databyteSeries` then I want to display the first one in that "collection" on the page. What I then need to do is display all other databytes that are not part of a collection. You can see an example here: http://graph.domaintesting.me/databytes hopefully that makes sense.  
> **username_2**: Do these two queries need to happen in the same page?  Or can we break this up into two problems 1) Display the first databyte entry in each databyteSeries, 2) Display all databytes that do not belong to a databyteSeries ?  
> **username_0**: Hi Ben, yes these two queries need to run on the same page I have added my attempt at a solution below. It seems to work in a simple test I have run, just need to add it to the main site template now.  
> **username_2**: Hey username_0, based on your clarification I've updated my answer above. Please consider the possible solution outlined in the 'UPDATE' section.  If that works and is more on target, I can clean up the rest of my answer and get rid of the code around my initial misunderstanding.  
> **username_0**: Hi Ben, the updated code looks like its just what I am after only for some reason its outputting all the databytes. I have tested the `alreadyUsedDatabytes` variable and that is working but for some reason `{% set unusedDatabytes = databytes|without(alreadyUsedDatabytes) %}` still outputs all the databytes with non of the `alreadyUsedDatabytes` removed.  
> **username_2**: Ok, one more try, I just added .indexBy('id') to the `databytes` and `alreadyUsedDatabytes` queries. I'm hoping that helps the `without` filter recognize what needs to match up.  
> **username_0**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/15565/discussion-between-gareth-redfern-and-ben-parizek).  

---

### Answer by username_3 on 2014-07-06. Score: 1

I think you should consider using a structure for your entries. Try a structure with Max Levels = 2 and two field types: single and series. Organize the structure like this:

single
single
series

single
single

etc.

Then to loop over the entries:
{% for dataByte in craft.entries.section('dataByte').level(1) %}
  {% switch dataByte.type %}
  {% case 'single' %}
      {# TEMPLATE CODE FOR STAND ALONE ENTRY HERE #}
      single: {{dataByte.title}}
  {% case 'series' %}
    {% set firstInSeries = dataByte.getChildren().first() %}
    {% if firstInSeries %}
      {# TEMPLATE CODE FOR FIRST ENTRY IN SERIES HERE #}
      seriesFirst: {{firstInSeries.title}}
    {% endif %}
  {% endswitch %}
{% endfor %}

> **Comments:**
> **username_0**: Thank you Marion, I hadn't looked at this for an option. I have now come up with a solution that is a mixture of my answer and Ben's but I appreciate your help and will definitely consider this in the future.  

---

