I have been doing a lot of searching on how to get an events list. Simple concept right and there should be a plugin right? Well, there are plenty of plug-ins. But due to the age of the tech on the host server, I was having a lot of trouble because the client’s host is still on PHP 4.# and mySQL 4.#… hey, it’s old!
Requirements:
- Event can span more than one day.
- Sort (3) events by those soonest to today’s date.
- If there are more than 3 events, show a link to View All Events
- Show mulit-day events – sort the list if the end date is before today’s date.
- Sort multi-day events by those soonest to today’s date, but if the start date has already past, still sort by that date or begin date.
- If the start and end year is the same, only display the year in the end date.
- If the start and end month is the same, only display month in the end date.
- List goes in sidebar.php
Simple Solution (still took a lot of hours to resolve):
- Create a category with slug “events” (used for archive) and (maybe or) change the “WHERE $wpdb->term_taxonomy.term_id = 9″ numeric value to your category(cateogries – yes even multiple).
- Create a post and add it to the events category.
- Create a custom field labeled “start_date” with a format of YYYY/MM/DD.
- Create a custom field labeled “end_date” with a format of YYYY/MM/DD.
- Both fields required even if only a single day event.
- Use a double meta_key post query.
- LIMIT 4 in the mySQL query limits the results to 4 (change to your number or remove) – view the Wordpress query values in the codex.
Code:
<!--p $today = date("Y/m/d"); $querystr = " SELECT * FROM $wpdb->posts LEFT JOIN $wpdb->postmeta AS endDate ON( $wpdb->posts.ID = endDate.post_id AND endDate.meta_key = 'end_date' ) LEFT JOIN $wpdb->postmeta AS startDate ON( $wpdb->posts.ID = startDate.post_id AND startDate.meta_key = 'start_date' ) LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE $wpdb->term_taxonomy.term_id = 9 AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->posts.post_status = 'publish' AND endDate.meta_value >= $today ORDER BY startDate.meta_value, startDate.meta_value ASC LIMIT 4 "; $eventPosts = $wpd-->get_results($querystr, OBJECT); ?> <!--p $eventCount=0; if ($eventPosts) :--> <!--p foreach ($eventPosts as $post):--> <!--p setup_postdata($post);--> <!--p if ($eventCount<3) :--> <!--p if(get_post_meta($pos-->ID, 'end_date', true)) { $event_end_date = get_post_meta($post->ID, 'end_date', true); $endDate = date("j F Y", strtotime($event_end_date)); $displayEndDate = date("j F", strtotime($event_end_date)); $endYear = date("Y", strtotime($event_end_date)); $endMonth = date("F", strtotime($event_end_date)); } else { $endDate = $startDate; } if(get_post_meta($post->ID, 'start_date', true)) { $event_start_date = get_post_meta($post->ID, 'start_date', true); $startDate = date("j F Y", strtotime($event_start_date)); $displayStartDate = date("j F", strtotime($event_start_date)); $startYear = date("Y", strtotime($event_start_date)); $startMonth = date("F", strtotime($event_start_date)); } else { $startDate = $endDate; } ?> <li><a title="<?php the_title_attribute(); ?>" rel="bookmark" href="http://www.est322.com/wp-admin/%3C?php the_permalink(); ?>"><span class="date"> <!--p if( $endDate!=$startDate) { if( $endYear==$startYear ){ if ( $endMonth==$startMonth){ $displayStartDateAlt = date("j", strtotime($event_start_date))." - "; echo $displayStartDateAlt; } else { $displayStartDateAlt = date("j F", strtotime($event_start_date))." - "; echo $displayStartDateAlt; } } else { echo $displayStartDate." - "; } } echo $displayEndDate; ?> </span> </a></li> <li>No Upcoming Events</li> » </span></a><a href="http://www.est322.com/wp-admin/%3C?php echo get_option('siteurl'); ?>/category/events/">View All Upcoming Events</a> <!--p endif;-->
I will follow up with another post which I leverage the same query for my archive.php and just include a simple is_category() to run the code above for those posts in the Events category only.
I am sure this could be simplified, especially all the break-down of the date values into year month evaluations.
Also if you read above, you know it works for php4.+! Hoorah! (Stupid old host server technology.)
2 comments so far.
whoisfelix said on February 15, 2010 at 7:30 pm
Haha! I don’t understand the means but I agree that there should be mo-fresher calendars on Wordpress. I’m admittedly jealous of your coding skills.
Leave a Comment

Logan said on February 11, 2010 at 2:53 am
Wat.