MODX Community

Filter articles by publish date

I try to filter the articles by publication dates by comparing two dates:

    [[!#GET.year:notempty=`
        [[!getResources?
            &parents=`157`
            &tpl=`ListBlogTpl`
            &where=`{"publishedon:date=`%Y-%m`:=": "2020-09" }`
        ]]
    `]]

This is an example of what I would like to do to filter articles by publication dates.

The publishedon-date is stored in the database as a timestamp, so you have to account for that.
You could try something like this:

Add a snippet getTimestamp to create a timestamp from a date string:

$date_str = $modx->getOption('date',$scriptProperties);
return strtotime($date_str);

Then use this snippet in the call to getResources:

[[!getResources?
    &parents=`157`
    &tpl=`ListBlogTpl`
    &where=`{"publishedon:>=": [[!getTimestamp? &date=`2020-09-01`]], "AND:publishedon:<": [[!getTimestamp? &date=`2020-10-01`]]}`
]]
1 Like

This seems to work too:

[[!getResources?
    &parents=`157`
    &tpl=`ListBlogTpl`
    &where=`"DATE_FORMAT(FROM_UNIXTIME(publishedon), '%Y-%m') = '2020-09'"`
]]
2 Likes