Skip Navigation

[Resolved] Checkbox filter to show posts from past 7 days

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by davidZ 4 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1472039

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!

#1473377

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.

#1473673

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

#1473817
Screen Shot 2020-01-22 at 1.59.29 PM.png

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/

#1474157
Screen Shot 2020-01-22 at 5.37.32 PM.png

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?

#1474197

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.