Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Last active January 8, 2016 19:09
Show Gist options
  • Save barryhughes/5e2b04e57d1f407a8e2f to your computer and use it in GitHub Desktop.
Save barryhughes/5e2b04e57d1f407a8e2f to your computer and use it in GitHub Desktop.
This query attempts to delete expired events with special handling for recurring events. Use at own risk!
-- Delete expired events.
--
-- Use at own risk and consider running on a test set of data first of all!
-- As is, should remove all events that have expired by more than one week,
-- with the exception of parent events for recurring events which will be
-- left in place.
--
-- Assumes a standard table prefix of "wp_" - adjust as required to match
-- your installation.
DELETE FROM wp_posts
WHERE post_type = 'tribe_events'
-- Special handling for recurring events
AND (
-- Expired children? Safe to remove
post_parent > 0
-- Expired non-children? Remove only if they are not recurring events
OR ID NOT IN (
SELECT DISTINCT( post_id )
FROM wp_postmeta
WHERE meta_key = '_EventRecurrence'
-- The following serialized data means "I am not a recurring event"
AND meta_value <> 'a:3:{s:5:"rules";a:0:{}s:10:"exclusions";a:0:{}s:11:"description";N;}'
)
)
-- Consider expired events to be ones that ended more than one week ago
-- (adjust interval if needed)
AND ID IN (
SELECT DISTINCT( post_id )
FROM wp_postmeta
WHERE meta_key = '_EventEndDate'
AND meta_value < ( NOW() - INTERVAL 1 WEEK )
)
@barryhughes
Copy link
Author

Note: it's possible that long-lived installations will include non-recurring events with a slightly different _EventRecurrence string than is anticipated above. Therefore if expired, non-recurring events are not removed on first run that would be worth looking at.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment