<p>So, this is what I ended up with, thanks @douglas-mcdonald and @carlcs</p>

<pre><code>{% set stuck = craft.entries.section('meetings').relatedTo(subcategory).sticky('1').order('meetingDate desc').find() %}
{% set entries = craft.entries.section('meetings').relatedTo(subcategory).order('meetingDate desc').limit(2).find() %}
{% set idArray=[] %} 
{% for entry in entries %}
    {% set idArray = idArray|merge([entry.id]) %}
{% endfor %}
{% for entry in stuck %}
    {% set idArray = idArray|merge([entry.id]) %}
{% endfor %}
{% set unique=[] %} 
{% for entry in idArray %}
    {% if entry not in unique %}
        {% set unique = unique|merge([entry]) %}
    {% endif %} 
{% endfor %}
{% set publish=[] %}
{% for entry in unique %}
  {% set publishItem = craft.entries.id(entry).first() %}
  {% set publish = publish|merge([publishItem]) %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-05-01T11:03:06.903Z
I think you are doing more work than needed. You can grab the ids using the `ids()` method, and then merge them if not already in array. Then grab all the entries properly sorted by meetingDate using the ids. See [updated answer](http://craftcms.stackexchange.com/a/9543/784).
Douglas McDonald
CC BY-SA 3.0
2015-05-01T20:41:48.140Z
Ah, right - thanks - I'm not that familiar with retrieving data - that is a lot simpler!
Jack McKenzie
CC BY-SA 3.0
2015-05-01T09:19:51.407Z
2015-05-01T09:19:51.407Z
Jack McKenzie
<p>One method is to check to see if item is not in array before merging. The downside is that the entries will just be appended vs sorted by meetingDate.</p>

<pre><code>{% set stuck = craft.entries.section('meetings').relatedTo(subcategory).sticky('1').order('meetingDate desc').find() %}
{% set entries = craft.entries.section('meetings').relatedTo(subcategory).order('meetingDate desc').limit(2).find() %}

{% for stuckEntry in stuck %}
    {% if stuckEntry not in entries %}
        {% set entries = entries|merge([stuckEntry]) %}
    {% endif %}
{% endfor %}
</code></pre>

<p>The other method is to grab the ids from the 2 queries and then merge them together, and perform a new query which can be sorted.</p>

<pre><code>{% set stuckIds = craft.entries.section('meetings').relatedTo(subcategory).sticky('1').ids() %}
{% set entryIds = craft.entries.section('meetings').relatedTo(subcategory).limit(2).ids() %}

{% for id in stuckIds %}
    {% if id not in entryIds %}
        {% set entryIds = entryIds|merge([id]) %}
    {% endif %}
{% endfor %}

{% set entries = craft.entries.id(entryIds).order('meetingDate desc') %}
</code></pre>

CC BY-SA 3.0
2015-05-01T01:46:31.227Z
Thanks, I already tried that - but for some reason stuckEntry always gets added to the entries array, so it ends up there twice. i.e. {% if stuckEntry not in entries %} gets evaluated as true so it seems that the array items are somewhat different.
Jack McKenzie
CC BY-SA 3.0
2015-05-01T02:47:53.127Z
That's very strange... I've definitely used that technique in the past. Worst case, you can get the ids, then do another query just using the ids.
Douglas McDonald
CC BY-SA 3.0
2015-05-01T06:49:36.210Z
Yes, it is, and I can't troubleshoot why - I used your code snippet exactly (which was in effect the same as mine). Your idea of using the ids works, but seems like a complicated workaround. But I thank you for offering me a solution.
Jack McKenzie
CC BY-SA 3.0
2015-05-01T07:26:29.633Z
The ID approach is the way to go if you want your last items sorted into that array of sticky posts and not just appended to it, @Jack.
carlcs
CC BY-SA 3.0
2015-05-01T00:53:53.837Z
2015-05-01T11:04:34.763Z
2015-05-01T11:04:34.763Z
Douglas McDonald
<p>I have a list of "meetings" as entries and I want to show only the last two ordered by meetingDate, and show any other meetings marked as sticky, without doubling up.</p>

<p>What I have so far is:</p>

<pre><code>{% set stuck = craft.entries.section('meetings').relatedTo(subcategory).sticky('1').order('meetingDate desc').find() %}
{% set entries = craft.entries.section('meetings').relatedTo(subcategory).order('meetingDate desc').limit(2).find() %}
{% set entries = entries|merge(stuck) %}
</code></pre>

<p>Now this works except that if an entry marked as Sticky is one of the first two, then it shows twice. I understand it is bad practice to try and manipulate data using Twig, and that rather I should aim on getting the right data out of the entries first up. </p>

<entries><lightswitch>
How do I limit entries retrieved except those with a sticky lightswitch on the entry?
# How do I limit entries retrieved except those with a sticky lightswitch on the entry?

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

I have a list of "meetings" as entries and I want to show only the last two ordered by meetingDate, and show any other meetings marked as sticky, without doubling up.
What I have so far is:
{% set stuck = craft.entries.section('meetings').relatedTo(subcategory).sticky('1').order('meetingDate desc').find() %}
{% set entries = craft.entries.section('meetings').relatedTo(subcategory).order('meetingDate desc').limit(2).find() %}
{% set entries = entries|merge(stuck) %}

Now this works except that if an entry marked as Sticky is one of the first two, then it shows twice. I understand it is bad practice to try and manipulate data using Twig, and that rather I should aim on getting the right data out of the entries first up.

**Tags:** entries, lightswitch


---

## 2 answers

### Answer by username_1 on 2015-05-01. Score: 4 | Accepted answer

One method is to check to see if item is not in array before merging. The downside is that the entries will just be appended vs sorted by meetingDate.
{% set stuck = craft.entries.section('meetings').relatedTo(subcategory).sticky('1').order('meetingDate desc').find() %}
{% set entries = craft.entries.section('meetings').relatedTo(subcategory).order('meetingDate desc').limit(2).find() %}

{% for stuckEntry in stuck %}
    {% if stuckEntry not in entries %}
        {% set entries = entries|merge([stuckEntry]) %}
    {% endif %}
{% endfor %}

The other method is to grab the ids from the 2 queries and then merge them together, and perform a new query which can be sorted.
{% set stuckIds = craft.entries.section('meetings').relatedTo(subcategory).sticky('1').ids() %}
{% set entryIds = craft.entries.section('meetings').relatedTo(subcategory).limit(2).ids() %}

{% for id in stuckIds %}
    {% if id not in entryIds %}
        {% set entryIds = entryIds|merge([id]) %}
    {% endif %}
{% endfor %}

{% set entries = craft.entries.id(entryIds).order('meetingDate desc') %}

> **Comments:**
> **username_0**: Thanks, I already tried that - but for some reason stuckEntry always gets added to the entries array, so it ends up there twice. i.e. {% if stuckEntry not in entries %} gets evaluated as true so it seems that the array items are somewhat different.  
> **username_1**: That's very strange... I've definitely used that technique in the past. Worst case, you can get the ids, then do another query just using the ids.  
> **username_0**: Yes, it is, and I can't troubleshoot why - I used your code snippet exactly (which was in effect the same as mine). Your idea of using the ids works, but seems like a complicated workaround. But I thank you for offering me a solution.  
> **username_2**: The ID approach is the way to go if you want your last items sorted into that array of sticky posts and not just appended to it, @Jack.  

---

### Answer by username_0 on 2015-05-01. Score: 0

So, this is what I ended up with, thanks @douglas-mcdonald and @username_2
{% set stuck = craft.entries.section('meetings').relatedTo(subcategory).sticky('1').order('meetingDate desc').find() %}
{% set entries = craft.entries.section('meetings').relatedTo(subcategory).order('meetingDate desc').limit(2).find() %}
{% set idArray=[] %} 
{% for entry in entries %}
    {% set idArray = idArray|merge([entry.id]) %}
{% endfor %}
{% for entry in stuck %}
    {% set idArray = idArray|merge([entry.id]) %}
{% endfor %}
{% set unique=[] %} 
{% for entry in idArray %}
    {% if entry not in unique %}
        {% set unique = unique|merge([entry]) %}
    {% endif %} 
{% endfor %}
{% set publish=[] %}
{% for entry in unique %}
  {% set publishItem = craft.entries.id(entry).first() %}
  {% set publish = publish|merge([publishItem]) %}
{% endfor %}

> **Comments:**
> **username_1**: I think you are doing more work than needed. You can grab the ids using the `ids()` method, and then merge them if not already in array. Then grab all the entries properly sorted by meetingDate using the ids. See [updated answer](http://craftcms.stackexchange.com/a/9543/784).  
> **username_0**: Ah, right - thanks - I'm not that familiar with retrieving data - that is a lot simpler!  

---

