'product', 'posts_per_page' => -1, // Retrieve all products 'meta_query' => array( array( 'key' => '_wc_last_active', 'compare' => '<=', 'value' => $currentDate->modify('-' . $thresholdMonths . ' months')->format('Y-m-d H:i:s'), 'type' => 'DATETIME' ) ) ); // Retrieve WooCommerce products matching the query $products = new WP_Query($args); // Loop through the products and delete them if ($products->have_posts()) { while ($products->have_posts()) { $products->the_post(); wp_delete_post(get_the_ID(), true); } } // Reset post data wp_reset_postdata();