Skip Navigation

[Closed] Date Range Filter – Custom Field – Checkboxes

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.

This topic contains 7 replies, has 2 voices.

Last updated by Luo Yang 8 years, 7 months ago.

Assigned support staff: Luo Yang.

Author
Posts
#80558

I need to create a filter which will search for records with custom field date entry within a date frame. These will be checkboxes. How do I got about doing this?

Next 30 Days
Next 60 days
Next 90 Days
Next 120 Days

#80568

I guess my question will be what will be an example shortcode for this::
wpcf-dates-of-event BETWEEN NOW(),FUTURE_DAY(30)

#80842

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hi mathewC,

I assume you are going to do it in views filter form, It is not possible with Views, since views defaultly use a date picker for a Types date field.

You can try create your custom checkboxes field in HTML code, and put it into your filter form.

More help: https://toolset.com/documentation/user-guides/date-filters/

Regards
Luo

#80928

Luo,

That is precisely my question: How could I create a custom checkbox that I can place on the filter form with the above documentation? The filter controls don't provide this capability by itself and I can't find any examples anywhere.

Thanks so much!

#82236

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

mathewC, Are we talking about using multi-choice checkboxes or single-choice radio button, if is single-choice radio button, please try this:

1) edit your view, set filter as this:
Posts, ordered by post date, descending

2) click "View/Edit Meta HTML" button under "Pagination settings"
wrap following html code into [wpv-filter-controls] ... [/wpv-filter-controls]


<p>Dates of event: 
<label><input type="radio" name="dates-of-event" value="30"> Next 30 Days </label>
<label><input type="radio" name="dates-of-event" value="60"> Next 60 Days </label>
  ...
</p>

3) add php codes in your theme/functions.php:

add_filter('wpv_filter_query', 'date_range_filter_func', 10, 2);
function date_range_filter_func($query, $view_settings) {
    if($view_settings['view_id'] == 1098 && isset($_REQUEST['dates-of-event']))
	{
		$time = current_time('timestamp');
		$value = implode( ',',
			array($time, $time + $_REQUEST['dates-of-event']*24*3600)
		);
		$query['meta_query'][] = array(
			'key'=> 'wpcf-dates-of-event',
			'value' => $value,
			'type' => 'NUMERIC',
			'compare' => 'BETWEEN',
		);
	}
    return $query;
}

Please replace 1098 with your views post ID.

More help: wpv_filter_query
https://toolset.com/documentation/user-guides/views-filters/

If you need checkboxes, you need change the PHP codes as you want, checkboxes will pass array URL parameter.

#82951
4-24-2013 4-21-44 PM.png

luoy, i think we're getting close here! I've added checkboxes and when posted it returns no records. The custom field dates-of-event has multiple dates and time.

The code on the front-end:
<ul class="categorychecklist form-no-clear">
<li class="popular-category"><label class="selectit"><input type="checkbox" name="dates-of-event" value="30"> Next 30 Days</label>

<li class="popular-category"><label class="selectit"><input type="checkbox" name="dates-of-event" value="60"> Next 60 Days</label>

<li class="popular-category"><label class="selectit"><input type="checkbox" name="dates-of-event" value="90"> Next 90 Days</label>

<li class="popular-category"><label class="selectit"><input type="checkbox" name="dates-of-event" value="120"> Next 120 Days</label>

#82953
<ul class="categorychecklist form-no-clear">
<li class="popular-category"><label class="selectit"><input type="checkbox" name="dates-of-event" value="30"> Next 30 Days</label></li>

<li class="popular-category"><label class="selectit"><input type="checkbox" name="dates-of-event" value="60"> Next 60 Days</label></li>

<li class="popular-category"><label class="selectit"><input type="checkbox" name="dates-of-event" value="90"> Next 90 Days</label></li>

<li class="popular-category"><label class="selectit"><input type="checkbox" name="dates-of-event" value="120"> Next 120 Days</label></li>

</ul>
#83047

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

There is a bug in Types date field, I need you check using some mysql tools,
Run such a sql:

SELECT * 
FROM  `wp_postmeta` 
WHERE  `meta_key` =  'wpcf-dates-of-event'
LIMIT 0 , 30

Check if all value is stored in timestamp format, such as 1367539200

And in your case, I suggest you try single-choice radio button, since option "Next 30 Days" is included in option "Next 60 Days", this is more resonable. do you think so?

The topic ‘[Closed] Date Range Filter – Custom Field – Checkboxes’ is closed to new replies.