Skip to content

Instantly share code, notes, and snippets.

@tommyshellberg
Created September 5, 2018 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommyshellberg/aafec8ff631b2a938ba2829241ae7add to your computer and use it in GitHub Desktop.
Save tommyshellberg/aafec8ff631b2a938ba2829241ae7add to your computer and use it in GitHub Desktop.
delete meta keys associated with WC Bookings if the products are missing(avoids fatal errors)
add_action('wp', 'delete_meta_bookings_no_products');
function delete_meta_bookings_no_products() {
$booking_ids = get_posts( array(
'numberposts' => 100000,
'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'wc_booking',
'post_status' => get_wc_booking_statuses(),
'fields' => 'ids',
) );
foreach ($booking_ids as $id) {
$booking = get_wc_booking($id);
$product = wc_get_product($booking->product_id);
if(!$product) {
delete_post_meta( $id, '_booking_product_id', $booking->product_id );
delete_post_meta( $id, '_booking_order_item_id' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment