4

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!

7
  • 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, 2011 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, 2011 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, 2011 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, 2011 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, 2011 at 22:04

1 Answer 1

5

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.

5
  • 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, 2011 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, 2011 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, 2011 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, 2011 at 19:19
  • Hey, I find you answer very helpful, thanks for that! I wonder if I can implement such a thing and query the events to results only the events from today on (e.g. start_date >= TODAY)? Thanks :)
    – benjah
    Nov 21 at 13:12

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.