Producten in bulk verwijderen (WordPress PHP-API)
Naar navigatie springen
Naar zoeken springen
The solutions below are from 2019 and done through SQL.
This can be done much more carefully through WP-CLI, and with some proper parallelisation, without taking too much time. See Wp wc product delete for some really cool code!
get_posts werkt niet?
Het lijkt erop, dat je niet zomaar de ID's van alle producten kunt verkrijgen via get_posts
. Zie deze voorbeelden:
<?php # # Get all products and/or images ############################################################### # print "\n===================================================\n"; print "===================================================\n"; print "===================================================\n"; ############################################################### # Libraries ############################################################### # $pad = "/home/strompf/www/rt.dvb/"; require_once($pad . "wp-load.php"); # These libraries seem not to be needed ####################################### # # require_once($pad . 'wp-admin/includes/media.php'); # require_once($pad . 'wp-admin/includes/file.php'); # require_once($pad . 'wp-admin/includes/image.php'); ############################################################### # get_pages - Wrong function ############################################################### # // $pages = get_pages(); // print_r($pages); ############################################################### # get_pages - Wrong function ############################################################### # # Appearantly, products are not "pages" in the sense of this # function - You just don't get them! # // $args = array // ( // 'post_type' => 'product' // ); // $pages = get_pages($args); // print_r($pages); ############################################################### # get_posts - Prints only Dutch products! ############################################################### # # * The trick is, that if you don't specify otherwise, some # default values are in place, like # # * 'post_type' => 'page' # * 'numberposts' => 72 # # * I still only get 23 products - Exactly all Dutch products # but no translations # // $args = array // ( // 'numberposts' => 72, // 'post_type' => 'product' // ); // $products = get_posts( $args ); // print_r($products); ############################################################### # Delete! ############################################################### # // $args = array // ( // 'numberposts' => 1000, // 'post_type' => 'product' // ); // $products = get_posts( $args ); // print_r($products); // $i=0; // foreach ($products as $product) // { // $i++; // echo $i." Post-ID: ".$product->ID."\n"; // wp_delete_post($product->ID, true); // } ############################################################### # get_posts - Get translations - Not working ############################################################### # $args = array ( 'numberposts' => -1, 'post_type' => 'product', 'category' => 1 ); $products = get_posts( $args ); $i=0; foreach ($products as $product) { $i++; echo $i." Post-ID: ".$product->ID."\n"; }
$wpdb-oplossing
Dit script werkt wel. Het is een niveau lager:
############################################################### # DB-level ############################################################### # global $wpdb; $sql_1 = "DELETE FROM ".$wpdb->prefix."term_relationships WHERE object_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'product');"; $sql_2 = "DELETE FROM ".$wpdb->prefix."postmeta WHERE post_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'product');"; $sql_3 = "DELETE FROM ".$wpdb->prefix."posts WHERE post_type = 'product';"; $sql_4 = "DELETE relations.*, taxes.*, terms.* FROM ".$wpdb->prefix."term_relationships AS relations INNER JOIN ".$wpdb->prefix."term_taxonomy AS taxes ON relations.term_taxonomy_id=taxes.term_taxonomy_id INNER JOIN ".$wpdb->prefix."terms AS terms ON taxes.term_id=terms.term_id WHERE object_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type='product');"; try{ $wpdb->query($sql_1); $wpdb->query($sql_2); $wpdb->query($sql_3); $wpdb->query($sql_4); }catch(Exception $e){}