Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i'm looking for a good and easy way to have a custom post type "event" that can have multiple dates. (I have tested about 20 plugins). I think i have no problem building a metabox allowing the user to duplicate the field group "date and time" multiple times. But how would a query look like that creates a chronological list of events from the meta data?

(The project is a playing schedule for a theatre. Productions play on several dates.) Thank you!

share|improve this question
    
this depends on the way you are storing the data, every date and time as a field or all of the as an array? –  Bainternet Feb 24 '11 at 18:57
    
I haven't set up the metabox yet. If storing the data as an array is useful i will do it this way. I'm thinking of using WPAlchemy for duplicating fields: farinspace.com/wpalchemy-metabox –  Christoph Feb 24 '11 at 19:55
    
Think it may be better manageable to do it as a hierarchical post type. Event = first level, individual sessions = second level. –  wyrfel Feb 24 '11 at 20:18
    
BTW: Since you tested 20 of them, can you recommend one? I need to replace the old Event Calendar Plugin with something a little more modern. But i need a calendar widget that shows the events. –  wyrfel Feb 24 '11 at 20:19
    
@wyrfel: I want the administration of events to be as simple as possible. - A meta box with a date field - An option to duplicate the field - All dates saved as an array. –  Christoph Feb 24 '11 at 22:04
show 2 more comments

1 Answer

up vote 2 down vote accepted

Here's a plan:

  1. Store the dates as individual custom fields with the same meta_key (ex: start_date)
  2. JOIN the wp_posts table with the wp_postmeta table, without a GROUP BY (to allow the same event to appear more than once)
  3. ORDER BY start_date

The full query would look like this:

SELECT wp_posts.*, meta_value AS start_date
FROM wp_posts INNER JOIN wp_postmeta ON (ID = post_ID)
WHERE post_type = 'event'
AND post_status = 'publish'
AND meta_key = 'start_date'
ORDER BY start_date

PS: This requires you store the date in YYYY-MM-DD format, which you should do anyway, for compatibility with mysql2date() etc.

share|improve this answer
    
Thank you very much @scripu! I have to work on another project today, but i'll try your solution as soon as possible! –  Christoph Feb 25 '11 at 9:12
    
I don't understand how to save multiple dates of the same event with the same meta_key. Could you please point that out? Thank you! –  Christoph Feb 25 '11 at 13:15
    
Just call add_post_meta( $post_id, 'start_date', $date ); for each date. See codex.wordpress.org/Function_Reference/add_post_meta –  scribu Feb 25 '11 at 14:34
    
Perfect! It works! For now only with the WP built-in custom fields. I have to figure out how to do it with my own fields. Thank you very much, @scribu! –  Christoph Feb 26 '11 at 19:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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