10

I have a channel of events with a field ‘eventDate’ for the date/time of the event. Within that there are ‘types’ of events ‘eventType’. What would be the most efficient way of outputting the NEXT (future date from now) that is of the ‘eventType’ equal to ‘Green’?

So far i’ve got

{% for event in craft.entries.section('events').order('eventDate asc') %}

Which will output the entries in the correct order based on the eventDate, but can i filter the query even further to include the conditionals to only output if the date is in the future like this conditional:

{% if event.eventDate|date("c") > now|date('c') %}

And also include the conditional to only output if the eventType == ‘green’ like so:

{% if event.eventType == ‘green’ %}

So that i can set the ‘limit’ to whatever i want and ONLY have the entries that meet the conditionals be included before that limit is applied?

I know that i can use the 'Search' like so:

{% for event in craft.entries.section('events').search('eventType:green').order('eventDate asc') %}

But A. Is that the most efficient and B. How do i also include only entries that are in the FUTURE from NOW based on the eventDate field?

14

You're actually closer than you might think. All you need to do is add your field handles as parameters. I prefer to use the hash style of declaring parameters but either way will work, I just find this easier to read.

{% set params = {
  section : 'events',
  order : 'eventDate asc',
  eventDate : '>=' ~ now,
  eventType : 'green',
  limit : '10'
} %}

{% for event in craft.entries(params) %}
...
{% endfor %}

This method also allows you to easily modify the params hash for whatever reason. For example you might want to set the eventType value to be something else based on the value of a query parameter. To do that you could use the merge tag like so:

{% if craft.request.getParam('eventType') not empty %}

  {% set params = params | merge({
    eventType : craft.request.getParam('eventType')
  }) %}

{% endif %}
  • Shall you please show how to filter data with the condition "not equal to", suppose in above example i want to find row whose eventType is not equal to green, then how can I write, I have tried "-" for not but dont know why is not working, help me out – Neeraj Rathod Jul 7 '17 at 4:59
  • @NeerajRathod Simply add 'not' like so: { eventType : 'not green' } – Josh Angell Jul 7 '17 at 6:24
  • I found what is the actual issue, I have added fields in the section and section already have around 3000 entries. So when I am putting "not" its not giving desire result because fields which I have added right now that are not available in the entries which is already available in the section. Any solution for the same ? – Neeraj Rathod Jul 7 '17 at 6:37
  • I have came up the solution is - to create a method under controller where i'll update all the entries with the newly added fields so in this way all the entries will have newly added fields. Shall i go for it, or you have any other suggestion – Neeraj Rathod Jul 7 '17 at 6:39
  • @NeerajRathod That's exactly what I'd do - or you can just re-save the section and it will re-save all the entries automatically for you. – Josh Angell Jul 7 '17 at 6: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.