Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top