Query using custom field for event date not working
-
Searched all the posts here related to using custom event dates in queries and didn’t find an answer to my specific problem, hoping someone can help.
I have a custom post type for events, with custom fields to hold the event start date and end date.
I have query that finds all matching posts and lists them in order of their start date as long as the start date is a future date (old events don’t show), however I have two problems:
The events drop off on the date that they start even though I’m using <= in my query, not just < (they should display through that date and drop off on the following date); and
For multi-day events, they should remain in the list until the event has ended (the day after the date the event ends) but they still drop off the list on the date that they start;
Here’s my code:
global $post; $eventdate = get_post_meta($post->ID,'event_date_start',true); $eventdate2 = get_post_meta($post->ID,'event_date_end',true); $eventlocation = get_post_meta($post->ID,'event_location',true); $eventaddress = get_post_meta($post->ID,'event_location_address',true); $eventdirections = get_post_meta($post->ID,'event_location_directions',true); $eventcontact = get_post_meta($post->ID,'event_contact',true); $eventcontactph = get_post_meta($post->ID,'event_contact_phone',true); $eventcontactem = get_post_meta($post->ID,'event_contact_email',true); $eventnumdays = get_post_meta($post->ID,'event_number_of_days',true); $myposts = get_posts('post_type=event&meta_key=event_date_start&orderby=meta_value_num&order=ASC&posts_per_page=-1'); foreach ( $myposts as $post ) : setup_postdata( $post ); $today = time(); if ( $eventnumdays == "Multiple Days" ) { $exp_date = strtotime(get_post_meta($post->ID,'event_date_end',true)); } else { $exp_date = strtotime(get_post_meta($post->ID,'event_date_start',true)); } if ($today <= $exp_date) {
If the custom field for the event_number_of_days is set to Single Day, then there is no custom field created for the end date, so my attempts to use event_date_end failed, which is why I’m using the custom field for number of days instead to determine if the post should expire after the start date or after the end date.
This query does display the events correctly, just not >>through<< the date they need to be still on the list….
For example, I have an event going on this weekend – it should show on the list until after midnight on Sunday, since the event goes through Sunday, but since it started on Friday, it already dropped off the list on Friday morning.
What am I doing wrong?
- The topic ‘Query using custom field for event date not working’ is closed to new replies.