How do I search events between two set dates inside WP?

There two things that you need to do to make this happen:

1) Create metadata for the data for each event

2) Query for the posts using meta_query

For #1, you need to add a metabox that allows the user to add a date for the event. This data should be stored as metadata using the add_post_meta or update_post_meta. I would encourage you to read about adding metadata if you are not familiar with how to do it:

http://codex.wordpress.org/Function_Reference/add_meta_box
http://www.wproots.com/complex-meta-boxes-in-wordpress/

For #2, assuming that your have saved the date values in an orderable manner (e.g., YYYY-MM-DD), you can use the meta_query parameter of within a new instance of WP_Query to get the appropriate date range. This method assumes that your meta_key is “_my-datetime-from”. For instance, you can get the posts in October and November 2011 with the following:

// Set arguments for events
$start="2011-11-31";
$end = '2011-10-01';
$args = array(
    'post_type' => 'my-event-type',
    'posts_per_page' => -1,
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_key' => '_my-datetime-from',
    'meta_query' => array(
        array(
            'key' => '_my-datetime-from',
            'value' => array($start, $end),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        )
    )
);
// Make the query
$events_query = new WP_query();
$events_query->query($args);

Leave a Comment