Puddinq.com sharing knowledge

Remove WooCommerce orders

Remove WooCommerce orders

Sometimes you need to clean up. One way of making your tables smaller is to delete data that is not used anymore,.. old orders in WooCommerce could be an option.

<?php

require('../wp-load.php');

$args =  array(
    'posts_per_page' => 100,   
    'post_type'   => wc_get_order_types(),
    'post_status' => array_keys( wc_get_order_statuses() ),
 'date_query' => array(
        array(
            'after'     => '2007-01-01',
            'before'    => '2017-06-01',
            'inclusive' => true,
        ),
    ),
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
	echo '<ul>';
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		echo '<li>' . get_the_ID() . '</li>';
		wp_delete_post(get_the_ID(),true);
	}
	echo '</ul>';
	/* Restore original Post Data */
	wp_reset_postdata();
} else {
	// no posts found
}