Filter Results by a Date Field Dynamically with FacetWP

Hi guys,

In this tutorial we will see how we can filter the results of a list by a date field.

In the following tutorial we are going to use the following premium plugins and templates:

Advanced Custom Filed (ACF)

facetWP

DIVI

You have available for FREE FacetWP, Advanced Customs Fields and DIVI in the Premium Zone

Video Tutorial

Subscribe to my channel:  

Let's imagine that we are creating an events page and we need that once the event is finished it does not appear in the list of our website.

Something like this:

List of works in WordPress

As I said, we want that once the event has passed, it does not appear in the list.

There are many possible solutions, the one that has occurred to me is the following:

  • Create a Date field with ACF
  • Filter by that field the results in FacetWP

Create a Date field with ACF

Thanks to ACF we can create a date field, in my case I have created a field called Date Remove Work

ACF Date Field

When I fill in a work it looks like this:

Work Date Field

Why haven't I used the same date field of the work?

You may be wondering why I haven't used the performance date field.

I haven't done it for two reasons:

1st Reason for not using the field of the date of the performance

Because I would also have to take into account the time of the performance so as not to remove the work prematurely.

2nd Reason

A work can have several performance dates, so it would be difficult for me to take into account which one to use.

In order not to go crazy I thought it was better to use a new field for the date to make the work disappear.

Once I have the field ready I have to add the filter in FacetWP

Create Filter in FacetWP

To do this we go to Settings -> FacetWP and within the template (Templates) that we are using we go to the Query tab and add the following filter:

facetwp dynamic filter

In the next filter we tell FacetWP that if the date of removing the work is greater than the current date, the result is filtered, that is, it does not appear.

We use NOW so that the filter date is dynamic and takes into account the date of the moment the QUERY is executed.

This is the way I have come up with to create a dynamic filter.

Can you think of another way to do it? Share it with us 😉

Remember that you have FREE available FacetWP, Advanced Customs Fields and DIVI in the Premium Zone

Leave a comment