Creating a Date Search Form

Notes on an example done with a user to create a form that search between input start and end dates (or just one of those). You would need to adapt this to work for you by changing the form and field names.

The end result looks like:
Start end search

Assumptions:

  1. We have a form named “start_end” with fieldsĀ “start” and “end”. Users enter dates such as 01/02/2015.

We would like to use $_POST in a filter to collect the dates posted to the page and filter based on that. The problem is when first loading the page without any POST values you get an error or no results. In this case we create a wrapper short code which checks for the existence of POST values coming to the page and sets the inner short code “filter” value accordingly.

Using the plugin Shortcodes Actions and Filters, we create a shortcode named “start_end_wrapper” add code as follows:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$tmp = array();
if (isset($_REQUEST['start']) && $_REQUEST['start']) {
    // change strtotime(start) to strtotime(YOUR_START_FIELD_NAME)
    $tmp[] = "strtotime(start)>=strtotime({$_REQUEST['start']})";
}
if (isset($_REQUEST['end']) && $_REQUEST['end']) {
    // change strtotime(end) to strtotime(YOUR_END_FIELD_NAME)
    $tmp[] = "strtotime(end)<=strtotime({$_REQUEST['end']})"; } $startEndFilters = implode('&&', $tmp); if (isset($atts['filter'])) { $atts['filter'] = $atts['filter'] . '&&' . $startEndFilters; } else { $atts['filter'] = $startEndFilters; } $sc = $atts['shortcode']; $shortCodeString = '[' . $sc; unset($atts['shortcode']); foreach ($atts as $name => $value) {
    $shortCodeString .= " $name=\"$value\"";
}
$shortCodeString .= ']';
if ($content) {
    $shortCodeString .=  $content . '[/' .  $sc . ']';
}
// echo $shortCodeString; // DEBUG
echo do_shortcode($shortCodeString);

Also add the short code echo_post_var:

1
2
3
4
5
6
7
8
9
if (isset($atts['var']) && isset($_REQUEST[$atts['var']])) {
    if (isset($atts['checked']) && is_array($_REQUEST[$atts['var']]) &&
            in_array($atts['checked'], $_REQUEST[$atts['var']])
    ) {
        echo 'checked';
    } else {
        echo "value=\"{$_REQUEST[$atts['var']]}\"";
    }
}

On the search page we put the following. Change “form” name and “show” to match your form. Change “cfdb-table” to be any CFDB short code name.

1
2
3
4
5
6
7
<form action="" method="POST">
Start: <input name="start" type="text" [echo_post_var var='start'] /> 
End: <input name="end" type="text" [echo_post_var var='end'] /> 
<input type="submit" />
</form>
 
[start_end_wrapper shortcode="cfdb-table" form="start_end" show="start,end"][/start_end_wrapper]

 

  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.