Hi There,
When setting up a view filter of a date field I wish to use the date picker but as default values I want to display texts "dates from" and "dates to" instead of a date as currently when the view is initially loaded it displays all the posts instead of filtering by the default dates. This is fine except the search from still shows the default dates but obviously the results don't match. Can this be done?
Any help would be appreciated.
Thanks in advance.
Hello,
I assume you are using legacy editor to setup the post view.
In the custom search form, you can find and edit the date field search control shortcode, and setup the attribute "default_date", for example:
[wpv-control-postmeta field="wpcf-test-date" url_param="wpv-wpcf-test-date_max" default_date="FUTURE_DAY(3)"]
More helps:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-control-postmeta
attribute "default_date"
https://toolset.com/documentation/legacy-features/views-plugin/date-filters/
Yes I'm using legacy.
I understand the function of the default_date but it doesn't actually apply the selected dates to the view when the page initially loads which I thought is what would happen. I was just wondering if text could be used as a placeholder instead whilst still using the date picker function?
No, there isn't such kind of built-in feature within Toolset plugins, it needs custom codes, see below sandbox website:
Login URL: hidden link
1) Create a custom code snippet "default-day-filter":
hidden link
with below codes:
add_filter('wpv_filter_query', function($query, $settings, $view_id){
if($view_id == 69 && !isset($_GET['wpv-wpcf-test-date_min'])){
$_GET['wpv-wpcf-test-date_min'] = strtotime('+1 day');
}
return $query;
}, 1, 3);
Please replace 69 with your post view's ID, replace "test-date" with your custom date field slug
2) Test it in frontend:
hidden link
It works fine.
More helps:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
hidden link