4

How would you combine multiple logical expressions on a ElementCriteriaModel parameter? You could query twice and then merge the two returned arrays of entry models, but that probably wouldn't allow me to later use the {% paginate %} tag or methods like getNext().

Here's an example where I'd like to filter events starting in calender week 32 or 34:

{% set dateParamCw32 = 'and, >= 2014-08-04 00:00:00, < 2014-08-10 23:59:59' %}
{% set dateParamCw34 = 'and, >= 2014-08-18 00:00:00, < 2014-08-24 23:59:59' %}

{% set startDateParam = 'date range cw32 OR cw34' %}

{% set entries = craft.entries.section('events').startDate(startDateParam) %}
  • This is a complicated answer and would need to be answered by Pixel & Tonic. Each condition goes through Craft's condition processor and then through Yii's condition processor before finalizing as a query. There's a lot of moving parts, and they would know those parts. – Bryan Redeagle Nov 5 '14 at 15:04
9

For most cases you can find a workaround to combine such conditions by making use of entry IDs.

In this example query for each time range condition separately and use the ids() method to "collect" your entries. All matching IDs are then passed to the id parameter of a final ElementCriteriaModel, that you can also use for Pagination etc.:

{% set dateParamCw32 = 'and, >= 2014-08-04 00:00:00, < 2014-08-10 23:59:59' %}
{% set dateParamCw34 = 'and, >= 2014-08-18 00:00:00, < 2014-08-24 23:59:59' %}

{% set entriesCw32Ids = craft.entries.section('events').startDate(dateParamCw32).ids() %}
{% set entriesCw34Ids = craft.entries.section('events').startDate(dateParamCw34).ids() %}

{% set idParam = [] %}
{% set idParam = idParam|merge(entriesCw32Ids) %}
{% set idParam = idParam|merge(entriesCw34Ids) %}

{% set entries = craft.entries.id(idParam) %}
| 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.