I have a function that rewrites the URL before post slug including a custom taxonomy

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

add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );

The function works just fine and replaces %nachrichtenkategorie% in URL when accessing from the archive

Getting the permalink of a post with get_permalink(123) doesn't replace the %nachrichtenkategorie% in the url therefore gets into server error.

What am I doing wrong? Maybe the filter post_type_link should be changed in something else that replaces that string in the url before displaying it. Any ideas? Thanks

  • 1
    If you can help me understand the bigger picture of what you are trying to do, perhaps I can provide a solution. The issue is that "uncategorized" is a default, necessary WordPress category and an integral part of WordPress. Removing it from the URL structure is not recommended. That's why I'm asking. – Ted Stresen-Reuter May 20 '19 at 9:30
  • 1
    Does post 123 have some term from nachrichtenkategorie taxonomy? – nmr May 20 '19 at 9:34
  • @TedStresen-Reuter get_permalink by post id if i'm not on the archive page gives this dead page and get_permalink on article page gives this page that is good. So I'm trying to understand why. – Razvan Cuceu May 20 '19 at 12:46
  • 1
    I can see your difficulty but without seeing more, like the relationship between the categories, all the categories, etc., it's very hard to say what's failing. I do have two questions: 1) What evidence do you have that the second link "is working" and not just a result of your filter not running (or running differently than what you expect) and 2) have you considered not testing for $terms (just do the string replacement directly - if it is there, it will be replaced, if not, it won't - less code, more joy)? – Ted Stresen-Reuter May 20 '19 at 13:08
  • 1
    @TedStresen-Reuter it could have passed a million years and I didn't tried 1). That led me to the solution. Actually found out that the post didn't had a term in that taxonomy and I was a fool not to think about, but now I added a default replacement for that so I don't encounter any errors in the future if( $terms ){ return str_replace( '%nachrichtenkategorie%' , $terms[0]->slug , $post_link ); }else{ return str_replace( '%nachrichtenkategorie%' , 'neuigkeiten' , $post_link ); } Thank you. – Razvan Cuceu May 20 '19 at 13:42

The code you are using looks valid, so…

Before going any further, check to see that the post you are testing with actually has the term you are expecting to find (and replace). In other words, verify your input is valid.

Also, you might consider just doing a straight string replace on the URL as, if the search term doesn't exist, no harm will be done. That said, if the search term does exist in the post name, for example, it will have unintended consequences so, you might also consider making the search more specific (like adding / (slash) to the search term).

Assuming the input is valid, you might also consider making this change in your rewrite rules (in apache or nginx) rather than in PHP. The reason is if you ever move to a new theme, for example, the functionality won't change (and it might even be faster than processing every request in PHP).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.