Skip Navigation

[Resolved] Setting a date query filter

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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by katinaA 9 months, 2 weeks ago.

Assigned support staff: Luo Yang.

Author
Posts
#2288983

I am trying to set a date query filter to limit the results that display to content created within the last 30 days. When in the date filter, I'm seeing I can select posts with a published date after or equal to. That's what I need. But then I'm unclear on how to set the rest. I'm thinking I need to choose the Day filter and set it to PAST_ONE. But then I'm unclear on the syntax to use to define the date as being 30 days ago. I don't want it to be a fixed date. I want it to be 30 days ago relative to whatever today is. Looking at this page here:

https://toolset.com/documentation/user-guides/views/filtering-views-query-by-date/

It seems like I only need to make the Day filter show PAST_ONE and then add the number 30 as the value. But when I do that and save it, the page is then showing No items found . All my News Article posts were published just a few days ago, so they should be displaying. When I remove the data filter, the results show as expected. I'm not sure what I'm doing wrong here. Any help you can provide would be appreciated.

#2289187

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hello,

You can try with Views filter hook "wpv_filter_query", for example, add one item in your website:
Dashboard-> Toolset-> Settings-> Custom codes:

add_filter( 'wpv_filter_query', function($query_args, $view_settings, $view_id){
    if($view_id == 123){ // replace 123 with your view's ID
        $query_args['date_query'] = array(
            'column' => 'post_date_gmt',
            'after' => '30 days ago',
        );
	}
    return $query_args;
}, 99, 3 );

More help:
https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

#2289351

Looks like that is working. Thanks! After I activated the code, it showed the results for all the News Articles. I then went in and changed the post date on one of them to last year and it removed that one from the results. So I think it is working. Thanks a bunch!

I'm curious though, why did PAST_ONE and 30 not work?

#2291621

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Toolset Blocks/Views plugins are using WordPress built-in class WP_Query to query posts, Within Toolset View's UI, in the publish date filter, the "day" parameter is "Day of the month", see WP document:
https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

But in your case, it needs to setup as strtotime() compatible value: 30 days ago, so it needs to use filter hook "wpv_filter_query" to setup the date filters, see our document:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#2291625

My issue is resolved now. Thank you!

Understood on your explanation.