I'm trying to figure out how to add a checkbox that will update a URL parameter to check the date in a custom field and show only posts where that date is within the past week.
I found this thread, which is on the right track but not quite right:
https://toolset.com/forums/topic/custom-values-for-wpv_filter_query/#post-1138008
Any idea how this might work?
Thanks!
Hi, there's nothing exactly like this built-in to Views that will allow your Users to toggle a date-based filter on/off using a checkbox. One way I can think of you might accomplish something similar is to apply the date custom field Query Filter in wp-admin, so only posts with a custom field date in the last 7 days are displayed in the results. Then programmatically turn off that filter using the Views API whenever a custom checkbox input field in the Search and Pagination panel is checked.
For example, here is a custom checkbox field in a set of conditionals. The first conditional displays the checkbox field unchecked on the front-end, as it would appear when someone visits the page for the first time. The second conditional displays the checkbox checked on the front-end when the URL parameter corresponding to this field exists, as it would when someone submits the search form:
[wpv-conditional if="( '[wpv-search-term param="wpv_gen_check_filter"]' ne '1' )"]
<input type="checkbox" value="1" name="wpv_gen_check_filter" id="wpv_gen_check_filter" />
[/wpv-conditional]
[wpv-conditional if="( '[wpv-search-term param="wpv_gen_check_filter"]' eq '1' )"]
<input type="checkbox" value="1" name="wpv_gen_check_filter" id="wpv_gen_check_filter" checked="checked" />
[/wpv-conditional]
Here is an example of the PHP Views API wpv_filter_query showing how you would determine if the checkbox is checked or not when the search form is submitted:
add_filter( 'wpv_filter_query', 'tssupp_toggle_date_filter', 10, 3 );
function tssupp_toggle_date_filter ( $query, $view_settings, $view_id ) {
$views = array( 1234, 1123 );
if( in_array( $view_id, $views) ) {
$is_checked = isset( $_GET['wpv_gen_check_filter'] ) ? 1 : 0;
if ( $is_checked ) {
// the checkbox is checked
// do nothing, the date filter is applied automatically
} else {
// the checkbox is not checked
// add your custom code here to programmatically remove the date field query filter
}
}
return $query;
}
I'm not sure of your PHP knowledge so let me know if you need more information about removing a query filter in the PHP API. I would need to know more about the other front-end filters in this View to give you more direct guidance.
We don't actually need the checkbox. I just thought that would be easier, but I don't think so now...
Would it be possible to filter via a URL parameter? Such as:
&latestposts
or
$postssince=20200115 (where this number would be 7 days ago...)
Thanks
Yes, either of those is possible. The first option &latestposts would require custom code using wpv_filter_query, almost identical to what I shared above but with a different $_GET variable.
Something similar to the second one is actually built-in to the system and would probably require no custom code. Date fields are stored in the database as Unix timestamps, so instead of &postssince=20200115 you would use something like &postssince=1579046400. Then the Query Filter would be set up to respond to the URL param postssince, as shown in the screenshot here. The param name is arbitrary, you could change if you want.
Similarly to the second URL param option above, you could use a shortcode attribute to supply the date query filter criteria instead of a URL parameter. Just wanted to make sure you're aware of that option as well: https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/
It looks like option 2 might work.
A couple of caveats. The custom field that hosts the date we want to filter by is an ACF field. So it's not UNIX, it's Ymd.
I added a URL parameter filer (attached image.)
But when I add the URL parameter ?latest=20200116, which should return posts where the date is greater than one week ago, it doesn't filter the posts at all.
Do I have the filter set up wrong?
I created a shortcode to return a Ymd date of one week ago. Then I added Shortcodes in Menus and added a custom URL page/?latest=[my-shortcode]
Then using the filter above, I am able to filter by this URL parameter.