How can I display only resources from a specific year with getResources (Modx)?

338 Views Asked by At

I want to display only resources that are published in the year 2015.

I tried:

&where=`{"publishedon:":2015}`

But this is not working. Can anybody help me?

1

There are 1 best solutions below

4
On BEST ANSWER

publishedon is saved as unix time in the database. So you have to convert your date (2015) to unix time and check a date range with that.

Create a snippet named inyear with the following code:

<?php
$year = $modx->getOption('year', $scriptProperties, '');
$where = $modx->getOption('where', $scriptProperties, false);
$where = is_array($where) ? $where : json_decode($where, true);
$where = ($where) ? $where : array();

if ($year) {
    $where[] = array(
        'publishedon:>=' => strtotime('1-1-'.$year),
        'publishedon:<' => strtotime('1-1-'.($year+1)),
    );
}
return json_encode($where);

After this you could call getRessources with

&where=`[[inyear? &year=`2015`]]`

You could even add an additional where clause with the &where property of the inyear snippet.

&where=`[[inyear? &year=`2015` &where=`whatever_else_where`]]`