2

So I have a Entry type that has two custom fields on it. Both fields are integer fields. I'd like to make a query in PHP that gets the entries by two custom fields, both of which are integer values. Here is my code so far:

    $query = [
        'year' => $year,
        'quarter' => $quarter,
    ];
    $seasonBoxes = \craft\elements\Entry::find()
                 ->section('boxes')
                 ->search($query)
                 ->orderBy('dateCreated asc')
                 ->all();

Please note: $year is independent of the entry creation date so I can't use the date filters.

1 Answer 1

2

Fields in craft that are not element is query by something called element criteria model. In twig you do:

{% set result = craft.entries({
       section: 'boxes'
       orderBy: 'dateCreated asc'
       year: year,
       quarter: quarter
}).all() %}

In php

 $query = [
        'year' => $year,
        'quarter' => $quarter,
    ];
 $seasonBoxesQueryObject = \craft\elements\Entry::find()
                 ->section('boxes')
                 ->orderBy('dateCreated asc');
 $seasonBoxesQueryObject->year = $query['year'];
 $seasonBoxesQueryObject->quarter = $query['quarter'];
 $seasonBoxes = $seasonBoxesQueryObject->all();
1
  • Thanks Parry! This code works great! Commented Jul 16, 2019 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.