4

I have an for entry loop in which I need to look for two different field conditions. Separately they are:

craft.entries.eventStartDate('>=' ~ (now.date))

craft.entries.genericEventDate(':notempty:’)

How can I ask craft to look to see if either of these parameters is true?

Update

A work around is to pull the query without these paramaters and then run an if statement like:

{% if entry.eventStartDate >= now.date or entry.genericEventDate is not empty %}

This works but it's not really ideal since the query is pulling more entries than necessary.

4 Answers 4

3

The only possible core solution to query for these entries in Twig is to query for entry IDs first, process them with merge or without and then pass the IDs to the id parameter of a new ElementCriteriaModel.

Other than your solution implementing a conditional in the for loop this works with paginate and getNext tags, you only query for the entry models you're after, but the cost of this is the extra queries you need to get the IDs.

{% set futureEventsIds = craft.entries.eventStartDate('>=' ~ (now.date)).ids() %}
{% set specialEventsIds = craft.entries.genericEventDate(':notempty:').ids() %}
{% set idParam = futureEventsIds|merge(specialEventsIds) %}

{% set allEvents = craft.entries.id(idParam).order('GREATEST(eventStartDate, genericEventDate) desc') %}
2

Hmm, I can't think of how you would do this in Twig. But you may want to jump into PHP and perform your query a little more manually... This answer provides a few good ways to do that.

Specifically, I'd take a look into either a custom variable or a template hook. I'm personally a huge fan of the template hook... it feels like unlimited power in a tiny package.

Once you're in PHP, you can use Craft's buildElementsQuery along with Yii's DbCommand to generate your own complex query. For a rough example, check out this question and answer...

2
  • I'm with you with this option but is it too much to ask to do this inline as just a part of the craft.entries query? Commented May 15, 2015 at 15:27
  • 1
    Agreed, a Twig-based solution would be preferred. But I don't have one, so I figured I'd leave you with this alternate approach instead. :)
    – Lindsey D
    Commented May 15, 2015 at 17:24
1

Many years later - a solution by pulling the queries separately, then merging the arrays and sorting them. Docs here

{% set events      = craft.entries().section('events').theMainCategory(entry.id).orderBy('startDate asc').all() %}
{% set eventsOther = craft.entries().section('events').otherCategories(entry.id).orderBy('startDate asc').all() %}
{% set events      = events|merge(eventsOther) %}
    
{% for event in events|sort((a,b) => a.startDate <=> b.startDate) %}
    {# Display event #}
{% endfor %}
0

I may be wrong here as only just started learning Craft and its template system but would this not work?

{% if (

    craft.entries.eventStartDate|date('U') >= now|date('U')
    or
    craft.entries.genericEventDate != ""
)
%}

<p>Show this text!!</p>

{% endif %}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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