Custom individual publication type with custom URL structure

I have a function that rewrites the URL before publishing after the inclusion of a custom taxonomy

wpa_show_permalinks function ($ post_link, $ post) {
if (is_object ($ post) && $ post-> post_type == & # 39; aktuelles & # 39;) {
$ terms = wp_get_object_terms ($ post-> ID, & # 39; nachrichtenkategorie & # 39;);
yes ($ terms) {
return str_replace (& # 39;% nachrichtenkategorie% & # 39 ;, $ terms[0]-> slug, $ post_link);
}
}
returns $ post_link;
}

add_filter (& # 39; post_type_link & # 39 ;, & # 39; wpa_show_permalinks & # 39 ;, 1, 2);

The function works well and replaces % nachrichtenkategorie% in the URL when accessing from the file

Obtain the permanent link of a publication with get_permalink (123) does not replace the % nachrichtenkategorie% in the url therefore gets into the server error.

What am I doing wrong? Maybe the filter post_type_link it must be changed into something else that replaces that string in the url before showing it. Any ideas? Thank you