Created
March 26, 2025 21:55
-
-
Save andrasguseo/d1011344b1ca497d206575f3443afdbc to your computer and use it in GitHub Desktop.
TEC/ECP > Delete orphaned recurring events after migrating to the new data structure introduced in TEC 6.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Deletes orphaned 'old' recurring events, after the events have been migrated. | |
* You know you have orphaned events if on the Event admin list the number of All events | |
* and the number of items don't match. | |
* @see https://dl.dropbox.com/scl/fi/tmljvc5fzzpr48arklsbt/shot_250326_225112.png?rlkey=dkg0qjh72zhafasnitbrfokv5&dl=0 | |
* | |
* Creating a database dump before and running a test on a staging site first is recommended! | |
* Creating a database backup before running the snippet on a production site is recommended! | |
* | |
* Usage: Add the snippet with Code Snippets as a "run once" snippet and run it repeatedly until all orphanes are deleted. | |
* (The All and the items match on the Event admin list.) | |
* OR | |
* Add the snippet with Code Snippets as a regular plugin and click on Dashboard. You will see an admin notice when | |
* all orphans have been removed. | |
* The snippet should be removed when done. | |
* | |
* @author: Andras Guseo | |
* | |
* Plugins required: The Events Calendar, Events Calendar Pro | |
* | |
* @since March 26, 2025 Initial version. | |
*/ | |
add_action( 'admin_init', function () { | |
if ( ! current_user_can( 'delete_posts' ) ) { | |
return; | |
} | |
global $wpdb; | |
$post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type='tribe_events' AND post_parent > 0 LIMIT 20" ); | |
if ( ! empty( $post_ids ) ) { | |
foreach ( $post_ids as $post_id ) { | |
wp_delete_post( $post_id, true ); // Force delete | |
} | |
// Redirect to trigger the next batch | |
wp_redirect( add_query_arg( 'deleted_batch', '1', admin_url() ) ); | |
exit; | |
} | |
// Add admin notice when no more posts are found | |
if ( isset( $_GET['deleted_batch'] ) ) { | |
add_action( 'admin_notices', function () { | |
echo '<div class="notice notice-success"><p>All orphaned child events have been deleted.</p></div>'; | |
} ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment