i'm trying to create a view that filters by posts created in the last 7 days. what are the exact settings to achive this?
Hello,
Yes, it is possible within Views filter hook "wpv_filter_query", for example, you can add below codes into your theme file functions.php:
add_filter('wpv_filter_query', function($query, $settings, $view_id){
if( in_array($view_id, array(123)) ){
$query['date_query']['after'] = date_i18n( get_option( 'date_format' ), strtotime( '-7 days' ) );
}
return $query;
}, 10, 3);
Please replace 123 with your post view's ID
More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.
https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
after (string/array) - Date to retrieve posts after. Accepts strtotime()-compatible string,
ok, thanks.
but with all the options you got in the views dialog, it's really not possible to do this without code? this is no problem to achieve using the date_query parameter (as in your example), but it can't be done directly in the views dialog? it seems that relative-to-today filters would be common. could this be a suggestion?
Sorry for the late answer, yes, you are right, there isn't such a built-in feature within Views plugin, see our document:
https://toolset.com/documentation/user-guides/filtering-views-query-by-date/
There isn't such an option to filter the posts by "created in the last 7 days", you can add a feature request for it:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
Our developers will evaluate it.