<?php
function wpas_clean_the_mess() {
	$posts = get_posts(  array(
		'numberposts'		=>	999,
		'post_type'			=>	array('tickets', 'ticket_reply', 'ticket_history', 'ticket_status'),
		'post_status'		=>	'any')
	);

	$deletable = array();

	// print_r($posts);

	foreach( $posts as $post ) {
		if( $post->post_parent == 0 )
			continue;

		$check = get_post( $post->post_parent );

		if( null == $check )
			array_push($deletable, $post->ID);
	}

	if( !empty($deletable) ) {
		foreach( $deletable as $key => $del ) {
			wp_delete_post( $del, true );
		}

		?><div id="message" class="updated below-h2"><p><?php printf(__('%s items cleaned.', 'wpas'), count($deletable)); ?></p></div><?php
	}
}

if( isset($_GET['clean']) && $_GET['clean'] == 'true' )
	wpas_clean_the_mess();
?>