Skip Navigation

[Closed] filter records based on custom date field

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 10 replies, has 2 voices.

Last updated by Nigel 1 year, 10 months ago.

Assisted by: Nigel.

Author
Posts
#2648887
SCR-20230929-rqkg.png

Tell us what you are trying to do?

I want to filter the records in my view by date, like the jobs sites do.

Past 24 hours
Past week
Past Month

Is there any documentation that you are following?

I looked at the support forum and documentation, but not sure how to even start.
https://toolset.com/course-lesson/filtering-toolset-views-by-dates/

Is there a similar example that we can see?

Linkedin Jobs, has a filter setup that way.
See image

What is the link to your site?
It is under development, so not available externally yet.

Appreciate the help.

#2649187

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screenshot 2023-10-02 at 10.25.14.png

Hi there

It's not possible without custom code.

Views filters only offer filtering by custom fields or taxonomies, not by standard fields (like post date or post modified).

So it is not possible to insert a filter for a standard field like post date.

It is possible to contrive a solution, which requires creating a dummy custom field to be able to add the UI for the select dropdown filter, and then custom code to hijack the query parameters and transform them into a date-based filter.

So, first add a custom field to a field group assigned to the post type being filtered.

This should be a select field, and for the options you will provide the texts you want to appear in the dropdown, and for the values you will use strings which can be used as data query parameters (WordPress data queries can be quite flexible in transforming texts into actual dates).

See the screenshot for an example of how you might set that up. (The following code depends on the custom field having a slug of show-results, but you can change it in both places as long as it is consistent.)

You then need to insert a filter for this custom field into your View.

Take a note of the ID of the View, which you will need for the custom code.

The code I added (via Toolset > Settings > Custom Code) was

function tssupp_date_filter($view_args, $view_settings, $view_id)
{

    if ( in_array($view_id, array( 123 )) && isset( $view_args['meta_query'] ) ) {
        
        foreach ($view_args['meta_query'] as $key => $filter) {
            
            if ( is_array($filter) && $filter['key'] == 'wpcf-show-results' )
            {
                $view_args['date_query'] = array(
                    'after'     =>  $filter['value'],
                    'column'    =>  'post_modified'
                );
                
                unset( $view_args['meta_query'][$key] );
                
                break;
            }
        }        
    }    
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_date_filter', 101, 3);

Note in that code example my View ID is 123.

You should be able to adapt this as needed for different date intervals etc.

#2649259

How do I find the View ID? Sorry for the basic question.

#2649265

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screenshot 2023-10-02 at 12.45.02.png

That's okay, it is not so easy if you are using the block editor.

You can momentarily switch the page being edited to the code editor, where you should be able to locate the View ID, as shown in the screenshot. (Then revert to the visual editor.)

Otherwise, if you go to Toolset > Settings and specify that both the block-based and legacy editors should be available, after refreshing the page you should be able to see the ID from the list of Views at Toolset > Views.

#2649303
SCR-20231002-hdtq.png

I got the ID thanks for that.

The date field I want to filter by is a custom field (see image) not the standard date posted field. That field is a date field.

My imports populate that field with a date. Is it possible to use the custom select to filter to determine the dates in the records based on the options I want. I am assuming that there must be date calculations to show my options.

I can provide login info to show you what I have going on.

Thanks again.

#2649321

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

You are importing the data into that field?

What format is the data in? Toolset date fields are stored as timestamps, is that the format the imported data has?

Because if this is essentially a normal Toolset date field then when you insert a filter for that field type in the View you can only use the format of a datepicker, which means users would select a date from a datepicker and you would use a comparison of "greater than or equals" for the filter.

#2649725
SCR-20231003-hcnz.png
SCR-20231003-hcgu.png

I am importing a csv file. and the dates are in US format (MM/DD/YYYY). 01/01/2023

Totally understand that I can use a date picker, but that really isn't the way folks would use a job search engine.

When searching for a job, users would normally pick options like the ones I mentioned.

Show me all jobs posted in the past 24 hours, etc.

The example I gave was linkedin job search. Included images from monster.com and linkedin.com

That is what I am trying to accomplish.

Thanks

#2649847

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, well in that format, it's not a date field (a Toolset date field).

It's essentially just a text field without any special meaning.

Can you transform it into a timestamp during the import process? (For example, WP All Import Pro let's you pass every import value through a callback function, which you could use to convert that US date into a timestamp, that could then be used as the basis for a date comparison via custom code to give you options similar to those you are looking for.)

#2649877

I'm sorry Nigel, I am not conveying my message correctly. My imported data is coming across as a date field.

What I was hoping to do is use the filter option like linkedin to filter the date field in my import.

So if I import 3 records, example below and filtered on past 24 hours, the posteddate field would be filtered to only show records posted within the past 24 hours. in the example below, only Job 1 would be displayed. ,

job title, posteddate
----------------------------
job 1, 10/03/2023
job 2, 10/01/2023
job 3, 08/28/2023

This is so much easier in my head. lol. Thank you for your patience.
if its easier to give you access to view the site or screenshots I can do either.

#2649887

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Let me get access details from you if that's okay so I can be sure exactly what I'm dealing with before recommending anything else.

I'll mark your next reply as private—you may want to create a temporary admin user for us to use that you can later delete. And be sure to have a current backup of your site.

Can you also confirm a post that has been imported with this data I can use as a reference?

#2650097

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, thanks for that.

I checked how the data are stored in the database and confirm they are in timestamp format.

The problem is that if you insert a filter for this date field into your View, the only format (other than plain text) available for users to input their search "date" is via a datepicker.

So you can't directly include a filter for this date field.

But you should be able to tweak the solution I proposed before. Make a dummy custom field that is a select field with a few options such as "24 hours", "1 week ago" or such like, and insert a filter for that field into your View.

We then use code to modify the query and swap out which field is being filtered to the actual date field, based on the selection for the dummy field.

(Remember, you won't actually be applying this dummy custom field to any of your posts, we are just using it to be able to generate a filter control in the View.)

If you create a show-results field as I described above and you are adding the filter to your View "Job Posts" which has an ID of 292, then I think this code should do what you need:

function tssupp_date_filter($view_args, $view_settings, $view_id)
{
 
    if ( in_array($view_id, array( 292 )) && isset( $view_args['meta_query'] ) ) {
         
        foreach ($view_args['meta_query'] as $key => $filter) {
             
            if ( is_array($filter) && $filter['key'] == 'wpcf-show-results' )
            {
                $view_args['meta_query'][$key]['key'] = 'wpcf-job-posted-date';
                $view_args['meta_query'][$key]['value'] = strtotime( $view_args['meta_query'][$key]['value'] );
                 
                break;
            }
        }        
    }    
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_date_filter', 101, 3);

The topic ‘[Closed] filter records based on custom date field’ is closed to new replies.