4

I am building a WP 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

If that won't be possible, is there any way I can get them to search for events happening between certain months, say December and February(excluding the days)?

Thanks in advance.

2 Answers 2

11

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);
2
  • Okay. Got you but how do I add it to the search-form so that I don't have to manually put in the dates in the code. It will be integrated into the search form so that users can get events by selecting dates. Thanks. Commented Nov 29, 2011 at 2:13
  • Okay I think I'm close. I have this in my search.php file now pastebin.com/zMRwRihz and this is what's in my search form: pastebin.com/nZePhPHV I am using jquery ui datepicker to select the dates in the frontend (date search input). This is what I get in the URL after submitting a search: localhost/wordpress3/…‌​2F31%2F2011&ghes_event_end_timestamp=&_wpnonce=4cb83de127 Still, no results are returned. Any clues what's preventing posts from showing? Thanks. Commented Dec 12, 2011 at 15:47
2

How do you store the event date: using the default post_date field of the post or a custom meta field?

If you're using the default post_date, you can use the filter posts_where to add conditions for search, like this:

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    // posts for March 1 to March 15, 2010
    $where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );

For more examples, please check out the Codex.

In case of using custom field, I guess you have to write your own custom MySQL queries. It's a little bit more complicated. Here's an example:

global $wpdb;
$post_ids = $wpdb->get_col( "
    SELECT ID FROM {$wpdb->posts}
    JOIN {$wpdb->postmeta}
    WHERE ID=post_id
    AND meta_key='your_custom_meta_key'
    AND meta_value >= '2010-03-01'
    AND meta_value < '2010-03-16'
" );

foreach ( $post_ids as $post_id ) {
    $post = get_post( $post_id );
    // Do something
}
wp_reset_postdata();
3
  • 1
    You don't have to use a custom query for this. You can just use "meta_query".
    – tollmanz
    Commented Nov 28, 2011 at 3:52
  • @rilwis Okay I think I'm close. I have this in my search.php file now pastebin.com/zMRwRihz and this is what's in my search form: pastebin.com/nZePhPHV I am using jquery ui datepicker to select the dates in the frontend (date search input). This is what I get in the URL after submitting a search: localhost/wordpress3/…‌​2F31%2F2011&ghes_event_end_timestamp=&_wpnonce=4cb83de127 Still, no results are returned. Any clues what's preventing posts from showing? Thanks. Commented Dec 12, 2011 at 15:48
  • you hardcoded the date values in. how do you make it dynamic, safely?
    – ahnbizcad
    Commented Sep 10, 2015 at 7:44

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.