3

I'm trying to show live entries to user by selecting list of months in a dropdown. Some my entries are live for 2 or 3 months. I having trouble in finding right method. I have tried using after and before but i think these work based on postdate of an entry. Correct me if i'm wrong. This all in template side.

2 Answers 2

4

"Live" is determined in part by postDate and expiryDate (in addition to status), so not technically independent, but this may be what you're after. You would still use the technique described in Carlcs' answer, only that the criteria would be searching the entries using the month values passed to your template.

{% if craft.request.param('startMonth') and craft.request.param('endMonth') %}

    {% set startMonth = craft.request.param('startMonth') %}
    {% set endMonth = craft.request.param('endMonth') %}
    {% set startDate = now|date(Y) ~ '-' ~ startMonth %}
    {% set endDate = now|date(Y) ~ '-' ~ endMonth %}

    {% set entries = craft.entries.section('mySectionHandle').postDate('and, >= ' ~ startDate ~ ', <= ' ~ endDate) %}

    {% for entry in entries %}
    ...
    {% endfor %}

{% endif %}

You could also use .after(starDate).before(endDate) for this.

Update If you want to get entries that are live during a particular month range (rather than posted during the month range) then you could check for postDate before the selected 'start month' and expiryDate after the selected 'end month'.

{% if craft.request.param('startMonth') and craft.request.param('endMonth') %}

    {% set startMonth = craft.request.param('startMonth') %}
    {% set endMonth = craft.request.param('endMonth') %}
    {% set startDate = now|date(Y) ~ '-' ~ startMonth %}
    {% set endDate = now|date(Y) ~ '-' ~ endMonth %}

    {% set entries = craft.entries.section('mySectionHandle').postDate('<= ' ~ startDate).expiryDate('>= ' ~ endDate) %}

    {% for entry in entries %}
    ...
    {% endfor %}

{% endif %}

Not tested.

3
  • Thanks for your quick response. I have tried this before, the problem with this is when i have entry (say: Foo) which is posted on jan with null on expiry date. For instance if i selected march from drop down with start and end date of a month then my Foo jan month entry will not showing Bcz it's post date is set to jan and there no expiry date and same with after and before. In all the above cases we are checking postdate which is not right solution in my case. I'm stuck here.
    – lokesh
    Apr 6, 2015 at 15:45
  • I see your problem. I added another example that may work (not sure). However, it doesn't account for entries that are live only for a short time within the month range. For that you may need to check for a postDate before the endMonth, and expiryDate after the startMonth. Apr 6, 2015 at 16:25
  • Thanks Douglas. Your updated answer works fine with some little modification based on my requirement.
    – lokesh
    Apr 21, 2015 at 16:27
3

For some ElementCriteriaModel parameters it is possible to define a range of possible values (→ Parameter Value Syntax). In case of your dropdown field you should be able to filter your entries for value between 1 (January) and 3 (March) like so.

{% set entries = craft.entries.myDropdownField('and, >= 1, <= 3') %}

You could also define a list of values like so.

{% set entries = craft.entries.myDropdownField('or, 1, 2, 3') %}
1
  • Thanks for your response. Sorry i didn't explain my question clearly. I'm working on template side. I have form with dropdown and i'm not storing my drop down into database. All months are in select drop-down form. After submission of the form i'm trying to display live entries of that month.
    – lokesh
    Apr 2, 2015 at 21: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.