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.
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/
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?
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
My issue is resolved now. Thank you!
Understood on your explanation.