• I am building a WordPress site with an events feature. The events page was made with custom post types.

    I want to make it possible for people to search/ filter events by dates. For example, they can search for all events happening between 2nd December, 2011 and 1st March, 2012 and get results from events that have their dates between the months of December and March (ie. december, january, february and march).

    I want to know the best way to go about this. Any ideas?

    You can see an example of what I want to achieve by looking at the “Search events by date” feature on this page http://www.londontown.com/events

    Here is the code for the start and end date meta boxes:

    $prefix = 'ghes_';
    
    add_filter( 'ghes_meta_boxes', 'ghes_sample_metaboxes' );
    
    function ghes_sample_metaboxes( $meta_boxes ) {
    
    	global $prefix;
    
    	$meta_boxes[] = array(
    		'id' => 'event_meta',
    		'title' => 'Event Metabox',
    		'pages' => array('event'), // post type
    		'context' => 'normal',
    		'priority' => 'high',
    		'show_names' => true,
    		'fields' => array(
    
    		array(
    				'name' => 'Event Start Date',
    				'desc' => 'field description (optional)',
    				'id' => $prefix . 'event_start_timestamp',
    				'type' => 'text_date_timestamp'
    			),
    
    		array(
    				'name' => 'Event End Date',
    				'desc' => 'field description (optional)',
    				'id' => $prefix . 'event_end_timestamp',
    				'type' => 'text_date_timestamp'
    			)

    Here is how the date data is saved:

    case 'text_date_timestamp':
    					echo '<input class="ghes_text_small ghes_datepicker" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? date( 'm\/d\/Y', $meta ) : $field['std'], '" /><span class="ghes_metabox_description">', $field['desc'], '</span>';
    					break;

    And this is what I have for my search box:

    <form action="<?php bloginfo('url'); ?>" method="get" id="searchform" class="form_search">
    
        		Event Search:
    
        		<!-- START Category Search -->
              <select name="event_cat">
                    <option value="0">Select...</option>
                    <?php
                    $theterms = get_terms('event_cat', 'orderby=name');
                    foreach ($theterms AS $term) :
                        echo "<option value='".$term->slug."'".($_GET['event_cat'] == $term->slug ? ' selected="selected"' : '').">".$term->name."</option>\n";
                    endforeach;
                    ?>
                </select>
    
        		<!-- END Category Search -->
        		<!-- START Date Search -->
        		<span class="em-events-search-dates">
        			<?php _e('between','ghes'); ?>:
    
        <input class="ghes_text_small ghes_datepicker" type="text_date_timestamp" name="ghes_event_start_timestamp" id="ghes_event_start_timestamp" value=""/>'
        			<input type="hidden" id="ghes_event_start_timestamp" name="start" value="" />
    
        			<?php _e('and','ghes'); ?>
    
        <input class="ghes_text_small ghes_datepicker" type="text" name="ghes_event_end_timestamp" id="ghes_event_end_timestamp" value=""/>'
        			<input type="hidden" id="ghes_event_end_timestamp" name="end" value="" />
        		</span>
        		<!-- END Date Search -->
        		<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce('search_events'); ?>" />
        		<input type="submit" value="<?php _e('Search','ghes'); ?>" class="em-events-search-submit" />
    </form>

    How do I get the form to process the “date search” data and display results between the dates users select?

    Is the search form missing anything? Is there something I should add to functions.php to make this work?

    I have been looking for an answer for almost two weeks now and really want to learn how this works. Please share your thoughts if you have an idea of how to get this to work.

    Thanks in advance.

  • The topic ‘Meta Query Help’ is closed to new replies.