1

Good morning, If have have a URL that passes parameters: http://url?month=January&Year=2018, how would I go about querying all entries in section where the postDate equals January 2018?

I can get the parameters by using: craft.app.request.getParam('month') and craft.app.request.getParam('year') but I can seem to figure out the correct way to only show me entries based on these parameters.

Thanks in advance.


I noticed a post that had the following answer and the response was that it worked. I tried it, and received the error: Object of class DateTime could not be converted to string.

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

{% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set lastDayOfMonth = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

{% set entries = craft.entries.section('yourSection').postDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}
2

Jon's answer is excellent, but if you want to keep the same month and year URL query params, I offer this.

The problem is that firstDayOfMonth and lastDayOfMonth are PHP class objects and can't be inlined as text. They need to be formatted as text with a date() or atom filter.

Here's your code with the filters added:

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

{% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set lastDayOfMonth = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

{% set entries = craft.entries.section('yourSection').postDate('and', '>= ' ~ (firstDayOfMonth|atom), '<= ' ~ (lastDayOfMonth|atom)) %}

You can make it extra readable by using the after and before methods since your query is based on Craft's postDate parameter.

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

{% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set firstDayOfNextMonth = firstDayOfMonth | date_modify('+1 month') %}

{% set entries = craft.entries
    .section('yourSection')
    .after(firstDayOfMonth|atom)
    .before(firstDayOfNextMonth|atom)
    .all()
%}

I'm sure you're aware, but it's worth noting for anyone finding this answer later: This is a bit fragile. If the URL query string does not have a valid month name and four-digit year number, it'll chuck errors.

| improve this answer | |
2

You'll want to use the dateCreated entry query: https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#datecreated. You'll also want to change the parameter to something like ?startdate=2018-01-01&enddate=2018-01-31.

{# create variables for start and end dates, getting values from params #}
{% set start = craft.app.request.getParam(startdate)|atom %}
{% set end = craft.app.request.getParam(enddate)|atom %}

{# loop through entries that match date created, and set loop to a variable #}
{% set entries = craft.entries()
    .dateCreated(['and', ">= #{start}", "< #{end}"])
    .all() %}
| improve this answer | |
1

I also found a solution. Seems to be working correctly. If I can simplify it, please let me know.

            {% set month = craft.app.request.getParam('month') ?? null %}
            {% set year = craft.app.request.getParam('year') ?? null %}

            {% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
            {% set lastDayOfMonth = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

            {% set firstDayOfMonth = firstDayOfMonth|date('Y-m-d') %}
            {% set lastDayOfMonth = lastDayOfMonth|date('Y-m-d') %}

            {% if month|length %}
                {# {% set newsEntries = craft.entries.section('news').postDate('and', '>= ' ~ firstDayOfMonth|date('Y-m-d'), '<= ' ~ lastDayOfMonth|date('Y-m-d')) %} #}
                {% paginate craft.entries.section('news').postDate(['and', ">= #{firstDayOfMonth}", "<= #{lastDayOfMonth}"]).orderBy('postDate desc').limit(10) as newsInfo, newsEntries %}
                <h3>{{ month}} {{ year }} Archives</h3>
            {% else %}
                {% paginate craft.entries.section('news').orderBy('postDate desc').limit(10) as newsInfo, newsEntries %}
                <h3>All Releases</h3>
            {% endif %}
| improve this answer | |

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.