Using taxonomy term in CPT permalink – Pages 404’s

Ended up with commenting out line 4 and changing the filter into this:

add_filter('post_type_link', function ( $post_link, $post, $id = 0 ) {
    if ( 'project' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post = get_post($id);  
    if (is_object( $post ) && $post->post_type == 'project' ) {
        $terms = wp_get_object_terms( $post->ID, 'department' );
        if($terms) {
            return str_replace( 'project' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;  
}, 1, 3);

Now pages works like they should again. All is well!