Custom Post Type “Event”: chronological list of recurring events

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.

Leave a Comment