0

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.

1 Answer 1

2

You should look into meta_query:

$args = array(
'meta_query' => array(
        'key' => 'event_start_timestamp', // the name of the meta
        'value' => '11/21/2011', // the value to search for
        'compare' => '>=', //search for start dates bigger or equal to value
    )
); 
$query = new WP_Query( $args );
8
  • Thanks for the answer. Where do I add this code; in the functions.php file or the search form? Also, should I leave the 'value' as it is (ie. 11/21/2011)? How do I get the date inputs on the search form to obey the meta_query? Please help if you can. Thanks. Dec 11, 2011 at 23:48
  • On the template page you want the search to appear, I think it's index.php in your code (bloginfo('url')). You add it right before before the loop. See here: codex.wordpress.org/Class_Reference/WP_Query Sorry for the short answer but im working on a demo on gist.github.com for you.
    – ferenyl
    Dec 12, 2011 at 2:02
  • Here is some code to put you in the right direction: gist.github.com/1464337
    – ferenyl
    Dec 12, 2011 at 2:20
  • Thanks. I actually have a search template (search.php) to display the search results and the search form is in a template I named (custom-search.php). Obviously, I didn't point my form's action correctly. Dec 12, 2011 at 2:22
  • This is awesome. Thanks. I'll let you know what happens. Thanks again. Dec 12, 2011 at 2:27

Your Answer

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

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