2

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.

4

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') %}
  • 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 May 1 '15 at 1:46
  • 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 May 1 '15 at 2:47
  • 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 May 1 '15 at 6:49
  • 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 May 1 '15 at 7:26
0

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

{% 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 %}
  • 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. – Douglas McDonald May 1 '15 at 11:03
  • Ah, right - thanks - I'm not that familiar with retrieving data - that is a lot simpler! – Jack McKenzie May 1 '15 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.