Skip Navigation

[Resolved] Filter view date from month and/or year

This thread is resolved. Here is a description of the problem and solution.

Problem:

The customer created a Custom Post Type (CPT) with a date field and wanted to allow users to filter posts by month and/or year using a select list. The Toolset view filters did not support filtering by only month or year because the date field is queried as a timestamp.

Solution:

We explained that it’s not possible to filter by month or year directly using Toolset’s default date field filters. As a workaround, we suggested creating separate custom fields for the month and year, which could then be used in the filter. We provided a code example using the save_post hook to automatically populate these custom fields based on the selected date whenever a post is created or edited:

function auto_generate_month_year_from_date_field($post_id) {
    // Check if it's the correct post type
    if (get_post_type($post_id) == 'YOUR_POST_TYPE') {
         
        // Retrieve the date field
        $date_field = get_post_meta($post_id, 'wpcf-YOUR-CUSTOM-DATE-FIELD-SLUG', true);
 
        if (!empty($date_field)) {
            // Convert the timestamp to a DateTime object
            $date_obj = DateTime::createFromFormat('U', $date_field);
 
            // Check if date creation was successful
            if ($date_obj) {
                // Extract the year and month
                $year = date_format($date_obj, "Y");
                $month = date_format($date_obj, "m");
 
                // Update the custom fields with year and month
                update_post_meta($post_id, 'wpcf-YEAR-FIELD-SLUG', $year);
                update_post_meta($post_id, 'wpcf-MONTH-FIELD-SLUG', $month);
            }
        }
    }
}
add_action('save_post', 'auto_generate_month_year_from_date_field', 101);

Relevant Documentation:
https://toolset.com/course-lesson/filtering-toolset-views-by-dates/
https://developer.wordpress.org/reference/hooks/save_post/
https://developer.wordpress.org/reference/hooks/save_post/

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 5 replies, has 2 voices.

Last updated by Mateus Getulio 3 months, 3 weeks ago.

Assisted by: Mateus Getulio.

Author
Posts
#2710461

Hi,
I created a CTP with a date (related by a document).

Now within my view I would like to give the ability to filter by month and/or year from a select list. How can I go about creating and managing this?

Regards

#2710617

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

Thank you for getting in touch.

Will this be a frontend filter or is it a backend filter? Assuming that this will be a frontend filter then it won't be possible to do. This is because the date field is queried as a timestamp which contains all the relevant information of that date such as month, day, year and time.

It is not possible with the view filters to filter by the custom field using the year and/or month only.

You're welcome to submit this as a feature request at:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

As a workaround, you could add separate custom fields to your post type to include the month and year separately, you can then use those fields to create your search based on the month and year.

Thanks,

#2710629

Hi,
I see that using the date du publication I have more granular management that on the other dates I do not have.

https://toolset.com/course-lesson/filtering-toolset-views-by-dates/

So I have created a function to update the publication date to my date. but now I need to figure out how to create a selection of months and years to port through with filter.
Regards

#2710770

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello Andrea,

I checked this with our team and you're correct when you say that the publication date field has more filters available.

However, it is not recommendable to edit this field as it can produce some unexpected behavior such as impacting the post visibility if it is set to a future date for example, and eventually other issues we can't predict.

I was told that the best workaround would be to create the custom fields for month and year and then use it in your filter.

To avoid duplication of efforts, what you can do is to use Cred Save Data hook if you're adding/editing the posts on the front end or the WordPress save_post hook if you're doing it in the back end using the WordPress editor to update those records for example. You can create a function that will break the custom field data and save the respective month and year to their isolated custom fields without you having to manually do it.

Please check these links for further reference:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data
https://developer.wordpress.org/reference/hooks/save_post/

If you need help with this function, please let me know if you're editing it on the back end or front end so that I can share an example.

Thank you, please let us know.
Mateus

#2710779

Hello Mateus,
I tried via the save_post hook but I can't save the custom fields. Can you provide me with an example?
Regards

#2710909

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello Andrea,

Sure thing, here's an example that I tested and did the trick for me:

function auto_generate_month_year_from_date_field($post_id) {
    // Check if it's the correct post type
    if (get_post_type($post_id) == 'YOUR_POST_TYPE') {
        
        // Retrieve the date field
        $date_field = get_post_meta($post_id, 'wpcf-YOUR-CUSTOM-DATE-FIELD-SLUG', true);

        if (!empty($date_field)) {
            // Convert the timestamp to a DateTime object
            $date_obj = DateTime::createFromFormat('U', $date_field);

            // Check if date creation was successful
            if ($date_obj) {
                // Extract the year and month
                $year = date_format($date_obj, "Y");
                $month = date_format($date_obj, "m");

                // Update the custom fields with year and month
                update_post_meta($post_id, 'wpcf-YEAR-FIELD-SLUG', $year);
                update_post_meta($post_id, 'wpcf-MONTH-FIELD-SLUG', $month);
            }
        }
    }
}
add_action('save_post', 'auto_generate_month_year_from_date_field', 101);

It will get the date from the field you point in the code, extract the month and year and save it to each field whenever you create/edit a post of that given post type.

Please give it a try and tell me how it goes.
Mateus