The Events Calendar – plugin to delete venues with no events

I created the function below to clean up venues that have no events associated. You can use a WordPress cron to run it periodically. For ease, I have created plugin you can download below. The plugin adds a “Delete Empty Venues” option to the settings page and runs the function.

function boltwood_delete_venues() {

    $venues = tribe_get_venues();

    foreach( $venues as $venue ) {

        if ( empty( tribe_get_events( array( 'venue' => $venue->ID ) ) ) ) {

            $to_delete[] =  $venue;
        }
    }

    foreach ( $to_delete as $delete ) {

        wp_trash_post( $delete->ID );
    }
}

I used wp_trash_post() rather than wp_delete_post() because venues are custom post types. I have discovered (in the creation of this function!) that using wp_delete_post() on a custom post type will delete the post entirely even with the default setting of ‘false’. This is because I prefer to double-check the venues before I delete them for good.

You can download the plugin here. If you find it useful, feel free to buy me a coffee.