0

I have a custom post type which creates events. These events have a start date and a end date. I want to be able to search for the events which occur on the specific day only. So if start date is 2018-10-25 and end date 2018-11-01 it would show up in the search, and if it has start date 2018-10-20 and end date 2018-10-22 it would not show up. Same thing with future events.

How should this be done? The post itself has the start-date and end-date in the metadata. But I don't know how to use this to modify the search form.

At the moment I use the standard search form:

<?php get_search_form(); ?>

1 Answer 1

0

Quick question...which case applies?

  1. 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.

  2. 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="%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.

2
  • It is number 1. I want the user to be able to search, and the results should only be what is active today. How is this done?
    – joq3
    Commented Nov 1, 2018 at 7:58
  • I added the widget code above. See if that does what you're looking for.
    – jdp
    Commented Nov 1, 2018 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.