{"id":1272,"date":"2015-02-09T13:52:52","date_gmt":"2015-02-09T18:52:52","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=1272"},"modified":"2016-06-11T13:29:02","modified_gmt":"2016-06-11T17:29:02","slug":"creating-a-date-search-form","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=1272","title":{"rendered":"Creating a Date Search Form"},"content":{"rendered":"<p>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.<\/p>\n<p>The end result looks like:<br \/>\n<a href=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2015\/02\/Screen-Shot-2015-02-09-at-2.17.53-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1275\" src=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2015\/02\/Screen-Shot-2015-02-09-at-2.17.53-PM-300x232.png\" alt=\"Start end search\" width=\"300\" height=\"232\" srcset=\"https:\/\/cfdbplugin.com\/wp-content\/uploads\/2015\/02\/Screen-Shot-2015-02-09-at-2.17.53-PM-300x232.png 300w, https:\/\/cfdbplugin.com\/wp-content\/uploads\/2015\/02\/Screen-Shot-2015-02-09-at-2.17.53-PM-1024x792.png 1024w, https:\/\/cfdbplugin.com\/wp-content\/uploads\/2015\/02\/Screen-Shot-2015-02-09-at-2.17.53-PM.png 1086w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Assumptions:<\/p>\n<ol>\n<li>We have a form named &#8220;start_end&#8221; with fields\u00a0&#8220;start&#8221; and &#8220;end&#8221;. Users enter dates such as 01\/02\/2015.<\/li>\n<\/ol>\n<p>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 &#8220;filter&#8221; value accordingly.<\/p>\n<p>Using the plugin <a href=\"https:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\" target=\"_blank\">Shortcodes Actions and Filters<\/a>, we create a shortcode named &#8220;<strong>start_end_wrapper<\/strong>&#8221; add code as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">$tmp = array();\r\nif (isset($_REQUEST['start']) &amp;&amp; $_REQUEST['start']) {\r\n    \/\/ change strtotime(start) to strtotime(YOUR_START_FIELD_NAME)\r\n    $tmp[] = \"strtotime(start)&gt;=strtotime({$_REQUEST['start']})\";\r\n}\r\nif (isset($_REQUEST['end']) &amp;&amp; $_REQUEST['end']) {\r\n    \/\/ change strtotime(end) to strtotime(YOUR_END_FIELD_NAME)\r\n    $tmp[] = \"strtotime(end)&lt;=strtotime({$_REQUEST['end']})\"; } $startEndFilters = implode('&amp;&amp;', $tmp); if (isset($atts['filter'])) { $atts['filter'] = $atts['filter'] . '&amp;&amp;' . $startEndFilters; } else { $atts['filter'] = $startEndFilters; } $sc = $atts['shortcode']; $shortCodeString = '[' . $sc; unset($atts['shortcode']); foreach ($atts as $name =&gt; $value) {\r\n    $shortCodeString .= \" $name=\\\"$value\\\"\";\r\n}\r\n$shortCodeString .= ']';\r\nif ($content) {\r\n    $shortCodeString .=  $content . '[\/' .  $sc . ']';\r\n}\r\n\/\/ echo $shortCodeString; \/\/ DEBUG\r\necho do_shortcode($shortCodeString);\r\n<\/pre>\n<p>Also add the short code <strong>echo_post_var<\/strong>:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">\r\nif (isset($atts['var']) && isset($_REQUEST[$atts['var']])) {\r\n    if (isset($atts['checked']) && is_array($_REQUEST[$atts['var']]) &&\r\n            in_array($atts['checked'], $_REQUEST[$atts['var']])\r\n    ) {\r\n        echo 'checked';\r\n    } else {\r\n        echo \"value=\\\"{$_REQUEST[$atts['var']]}\\\"\";\r\n    }\r\n}\r\n<\/pre>\n<p>On the search page we put the following. Change &#8220;form&#8221; name and &#8220;show&#8221; to match your form. Change &#8220;cfdb-table&#8221; to be any CFDB short code name.<\/p>\n<pre lang=\"html\" line=\"1\" escaped=\"true\">\r\n<form action=\"\" method=\"POST\">\r\nStart: <input name=\"start\" type=\"text\" [echo_post_var var='start'] \/> \r\nEnd: <input name=\"end\" type=\"text\" [echo_post_var var='end'] \/> \r\n<input type=\"submit\" \/>\r\n<\/form>\r\n\r\n[start_end_wrapper shortcode=\"cfdb-table\" form=\"start_end\" show=\"start,end\"][\/start_end_wrapper]\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: Assumptions: We have a form named &#8220;start_end&#8221; with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":102,"menu_order":5,"comment_status":"open","ping_status":"open","template":"page-without-sidebar.php","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-1272","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-kw","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1272"}],"version-history":[{"count":7,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1272\/revisions"}],"predecessor-version":[{"id":1276,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1272\/revisions\/1276"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/102"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}