Post filtering by year & month

Hi! Modx Community, Is this possible to display posts by year and then the month of that year.
When we will click on the year, all posts will display of that year and then if we click the specific month then will display the post of that month under the year.
For example, if I click 2020. It will display the posts from 2020 January to December. And then again click on the month January, it will display the posts from January 2020 only.

It’s certainly possible.

How to implement it exactly depends on how you store your data.
If your posts are normal MODX resources and you want to filter by the published date, then you can use the where property of getResources/pdoResources.

Generate the value of the where property in a custom snippet where you read the year and the month parameters from the request.

@halftrainedharry Thank you for replying.
I request you to look at the attachment. I need to display the years and when I will click on that, it will display entire posts of that year. Besides when I will click on the month that will be under the selected year. But if I click the different year then the month will reset again. I need clickable years and months that will filter on live. I am not supposed to mention explicitly the year and month.

So when a user clicks on a year or a month, you run some javascript code that reloads the page with request parameters (GET or POST) for the year/month or you load the data with AJAX (and the same request parameters).

Then in MODX you read these request parameters in a custom snippet and create the value of the where property for the getResources/pdoResources call.

Here is some code that may work:

[[!pdoResources?
    &parents=`1`
    &tpl=`myTpl`
    &limit=`0`
    &sortby=`publishedon`
    &sortdir=`desc`
    &where=`[[!getDateFilter]]`
]]

Snippet getDateFilter: Reads the values of the GET-parameters and create the where property.

<?php
$year = date('Y'); //current year as default
$month = 0;
if (isset($_GET['year'])) {
    $year = intval($_GET['year']);
}
if (isset($_GET['month'])) {
    $month = intval($_GET['month']);
}

if ($month > 0){
    //DATE_FORMAT(FROM_UNIXTIME(publishedon), '%Y-%m') = '2022-04'
    return "DATE_FORMAT(FROM_UNIXTIME(publishedon), '%Y-%m') = '" . $year . "-" . sprintf('%02d', $month) . "'";
} else {
    //DATE_FORMAT(FROM_UNIXTIME(publishedon), '%Y') = '2022'
    return "DATE_FORMAT(FROM_UNIXTIME(publishedon), '%Y') = '" . $year . "'";
}

Then call the page with the GET-parameters:

.../myposts.html?year=2022
.../myposts.html?year=2022&month=4

@halftrainedharry
Sorry, I am very ashamed to ask you repeatedly. I have tried your code with snippet “getDateFilter” but I am not understanding what to write in “myTpl”. Because I need to display years and months with links.
If you look at my attachment and make a solution including the tpl, that will be so much honoured for me. Because I am not much experienced.
Thanks in advance.

This list of years and months, do want it to only show year/month combinations that have a corresponding post?
Let’s say you click on the year “2021”, do you want to show all the months or only the months when a post was released?


The code I posted does not generate links. It only queries matching posts for the request-parameters year and month in the URL. myTpl is the template for one post in the result. This is the normal &tpl property for a pdoResources tag.


There is also an extra Archivist that kind of does what you need.

The current version is quite old and I don’t know if it still works, (but as it is used by the extra Articles it might be kept up to date.)

@halftrainedharry
I am familiar with Archivist extra and using it as well. But that doesn’t make my demand. I request you to look at the attachment carefully.
If I click the month “January” the posts will display from the month of January and the selected year.

When 2020 is selected and click on January. The result from “January 2020”.
When 2021 is selected and click on January. The result from “January 2021”.
When 2022 is selected and click on January. The result from “January 2022”.

You can’t assign a hardcoded link to the “January” element. When “January” is clicked you have to run some javascript code to read the current value of the year and then add the parameters to the url and reload the page. Something like:

var year = '2020';
var month = '1';
window.location.search = '?year=' + year + '&month=' + month;

The exact code depends on the specific markup of the filter area of your webpage.

I am very delighted to get your response. Thank you for your suggestion and get me your time.

Could you please help me with a tip?
How can I make this layout?
this is the single page content. this is not possible by RichText field. Is there any other solution?

We usually use ContentBlocks for this type of layout (which is a paid extra).

If you want something for free, then maybe you can use Fred or MIGX.

I’m not sure if this is possible for you, but if you organized the resources in folders with a folder for each year, and under it folders for each month. A Wayfinder call in your template could create your headings and links automatically.

You’d want to put the year into the alias for the month folders so you wouldn’t have duplicates aliases.