1

How do I filter entries by URL parameters. The url is /booking?month=november&year=2015.

So far I have:

{% set allEntries = craft.entries.section('availability').order('availabilityStartDate').limit(null) %}

where availabilityStartDate is a date field.

How do I return entries that match November and 2015.

Thanks for any ideas or code!

4

You can pull query string variables using the HttpRequestService, like this:

{% set month = craft.request.query('month') %}
{% set year = craft.request.query('year') %}

Then, you'll need to construct a DateTime object from those query parameters:

{% set queryDate = date([month, year]|join(' ')) %}

...and finally, include that date object in the criteria for craft.entries (notice the >= operator, which means we should get entries with a availabilityStartDate value equal or higher to the queryDate date object:

{% set allEntries = craft.entries.section('availability').order('availabilityStartDate').availabilityStartDate('>= ' ~ queryDate.date).limit(null) %}

EDIT

Filtering out entries with date field values matching the queried month and year exactly is a bit more complicated.

There are probably several ways to approach this, but a thing that works is to use both a start date (i.e. the first day of the queried month) as well as an end date (i.e. the last day of the queried month) for the criteria.

Being that the queryDate variable actually already refers to the first day of the queried month, we'll just need to add 1 month for the end date:

{% set queryStartDate = date([month, year]|join(' ')) %}
{% set queryEndDate = queryStartDate.date|date_modify('+1 month') %}

And here's the element criteria with both date object statements:

{% set allEntries = craft.entries.section('availability ').order('availabilityStartDate').availabilityStartDate('and', '>= ' ~ queryStartDate.date, '< ' ~ queryEndDate.date).limit(null) %}

Here's everything together, for cohesion:

{# Get month and year query parameters #}
{% set month = craft.request.query('month') %}
{% set year = craft.request.query('year') %}

{# Create DateTime objects for the start date and end date (+1 month) #}
{% set queryStartDate = date([month, year]|join(' ')) %}
{% set queryEndDate = queryStartDate.date|date_modify('+1 month') %}

{# Get the entries #}
{% set allEntries = craft.entries.section('availability ').order('availabilityStartDate').availabilityStartDate('and', '>= ' ~ queryStartDate.date, '< ' ~ queryEndDate.date).limit(null) %}

PS: When you have multiple parameters to craft.entries like this, you might want to consider the object syntax for readability:

{% set allEntries = craft.entries({
    section : 'availability',
    order : 'availabilityStartDate',
    availabilityStartDate : 'and, >= ' ~ queryStartDate.date ~ ', < ' ~ queryEndDate.date,
    limit : null
}) %}
  • Hi mmikkel, thanks for taking the time to answer me - much appreciated. I've used {% for entry in allEntries %} {{ entry.availabilityStartDate | date ("j F Y") }}<br>{% endfor %} to output the entries that should have month=november and year=2015.The output includes all entries in december 2015, but is also including january 2016, february 2016 - whereas I only want entries where month=november and year=2015. Got any ideas, I've tried several things to no avail. – user1070143 Nov 12 '15 at 17:44
  • Aha, I didn't catch that detail, sorry. You'll want to use the = operator over >=, but you'll also need to normalize the dates. I'm in a rush right now but I'll look at it later if you don't get another answer (or an edit) :) – Mats Mikkel Rummelhoff Nov 12 '15 at 18:26
  • Hi mmikkel, when you've got a moment help with normalizing the dates would be great. Have a good weekend! – user1070143 Nov 13 '15 at 11:17
  • Hello @user1070143 – check out the edit, hopefully this will work out for you. A good weekend to you to! – Mats Mikkel Rummelhoff Nov 13 '15 at 18:24
  • Hi mmikkel, thank you very much for the time and expertise you put into answering my questions. If you care to drop me your paypal email I'd like to buy you a bottle of wine. – user1070143 Nov 18 '15 at 20:54

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.