get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s ORDER BY post_id ASC", 'rank_math_schema_Off' ) ); $current_count = 1; $deleted_count = 0; $failed_count = 0; // Delete 'rank_math_rich_snippet' meta key and run delete_schema function for each product. foreach ( $product_ids as $product_id ) { WP_CLI::log( sprintf( 'Deleting rank_math_rich_snippet and rank_math_schema_Off for product %d of %d.', $current_count, count( $product_ids ) ) ); $current_count++; // Delete 'rank_math_rich_snippet' meta key. delete_post_meta( $product_id, 'rank_math_rich_snippet' ); $delete_schema_status = $this->delete_schema( $product_id ); // Run delete_schema function. if ( $delete_schema_status ) { $deleted_count++; } else { $failed_count++; } } $total_count = count( $product_ids ); $summary_message = sprintf( 'rank_math_schema_Off removed for %d out of %d WooCommerce products. %d deletions failed.', $deleted_count, $total_count, $failed_count ); WP_CLI::success( $summary_message ); } /** * Runs the delete_schema function for a given product ID. * * @param int $product_id The ID of the product to run delete_schema for. * @return bool True if successful, false if failed. */ public function delete_schema( $product_id ) { // Perform actions in the delete_schema function. global $wpdb; $where = $wpdb->prepare( 'WHERE post_id = %d AND meta_key LIKE %s', $product_id, $wpdb->esc_like( 'rank_math_schema_' ) . '%' ); $result = $wpdb->query( "DELETE FROM {$wpdb->postmeta} {$where}" ); // phpcs:ignore return false !== $result; } } if ( defined( 'WP_CLI' ) && WP_CLI ) { WP_CLI::add_command( 'delete-schema', 'Delete_Schema_Command' ); }