Created
September 5, 2018 15:49
-
-
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)
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
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