Quick question…which case applies?
-
If you just need a query for today’s date being between two pieces of metadata, I’d create a widget running a fairly straight forward WP_Query and outputting the posts.
-
If you are asking for the user to input a date to search, I’d probably still use a widget but would use the jQuery UI datepicker method for better user interface and data integrity.
Either way, I wouldn’t repurpose the stock search form (which is just a widget anyway).
OK…if it’s option 1, try this…
build the widget in your functions file.
class my_events_widget extends WP_Widget {
//custom properties
public $base_id = 'my_events';
public $title="My Events Title";
public $description = "Today’s Events";
//end
function __construct(){
parent::__construct($this->base_id,$this->title,['description'=>$this->description]);
}
public function widget($args, $instance) {
$str="<div class="widget"><h2>".$this->description.'</h2><ul>';
$q = new WP_Query([
'post_type'=>'events',
'meta_query'=>[
'relation'=>'AND',
[
'key'=>'start_date',
'value'=>date('Y-m-d'),
'type'=>'DATE',
'compare'=>'<='
],
[
'key'=>'end_date',
'value'=>date('Y-m-d'),
'type'=>'DATE',
'compare'=>'>='
]]
]);
if ($posts = $q->get_posts()) {
foreach ($posts as $post) {
$str .= sprintf('<li><a href="https://wordpress.stackexchange.com/questions/318118/%s">%s - %s</a></li>', esc_attr(get_permalink($post)), $post->post_title, get_the_date('M j, Y', $post));
}
} else $str .= "<li>Nothing posted currently. Please check back.</li>";
$str .= '</ul></div>';
echo $str;
}
}
Now register the widget in your theme’s functions file.
function my_custom_widgets() {
register_widget('my_events_widget');
}
add_action('widgets_init','my_custom_widgets');
You can put the widget in a sidebar or use it in a page/post as a template tag using the_widget()
function.
Might need to tweak the WP_Query a little since I couldn’t fully test it out but it should get the job done.