0

I am trying to filter a view to show content that happened today on this day in previous years.

We have day, month and year fields but they are not dates.

How can I filter on day = today's day and month = today's month without converting them to date fields.

Current fields are:

  • Day - integer
  • Month - List (integer)
  • Year - integer
2

You can do that with views php.

Construct your view to take two contextual filters. Do not append any placeholder (%) to the path.

First filter: Add the month field as a contextual filter. Select from the section that reads: When the filter value is not in the url, Provide a default value. A select list Type opens up and from it you select PHP Code and in the PHP contextual filter code you enter:

date('F');

This will match it against today's month in the format of a full string ie. January. Adjust it if your month format is different (if for 3 letter abbrev, use date('M');)

Test this by typing in the month for a node that exists in the Preview with contextual filters text box at the bottom and update preview. It should show you your nodes filtered by month.

Second Filter: Add the day field as a contextual filter, and as above select PHP Code for default value. Add this code:

date('j');

This will filter on the day in the format of 1-31. If you want 01-31 then use date('d');

Test this by typing in the month/day for a node that exists in the Preview with contextual filters text box at the bottom and update preview. It should show you your nodes filtered by month and day.

You can also access the view as such: VIEWPATH/MONTH/DAY and it will show you a result for the values you provided. If you just access the view by VIEWPATH it will give you the result for the date of today.

enter image description here

  • Though this was the solution it was a little trickier. The Block was being pulled into a template and was not honoring the default values. So I had to set them before calling the block and pass them in as args. Then it worked as expected. – dibs Aug 28 '14 at 3:28

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.