Delete child posts as soon as its parent is deleted

Deletes all Child Posts as soon a Parent Post is deleted. Always make a backup first, mainly when you are running DELETE queries.

add_action( 'delete_post', 'wpv_delete_child_posts', 10 );

function wpv_delete_child_posts( $pid ) {
    global $wpdb;

    $ids = $wpdb->get_results( "SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key = '_wpcf_belongs_company_id' AND meta_value = '".$pid."'" );
 	
    if(isset($ids)){
   foreach ($ids as $key) {
   	wp_delete_post($key->post_id, true);
   }
}

    return true;
}