Search code examples
wordpressurl-rewritingparent-childpermalinks

Delete in the permalink Wordpress taxonomy father


I put a personalized taxonomy with a plugin in the permalink of Wordpress. This taxonomy is hierarchical. The child has two levels above him. I just want to leave my son. Example: Africa/Algeria/Algeri I just want: Algeri

My current URL is as follows: www.example.com/guide/africa/algeria/algeri/nome/ID

I want it to be like this: www.example.com/guide/algeri/nome/ID that is, you just leave your son

The URL (www.example.com/guide/algeri/nome/ID) is working but is not changed in the back office and Wordpress front office despite being "False" in the Rewrite of the Taxonomy (use CPT UI plugin). I always see the original URL.

In the Wordpress permalink I wrote: guide/%taxonomy%/%postname%/%post_id%

How can I fix it?


Solution

  • You can't do it with permalinks strings. But WordPress filters can help you. There is a filter called "post_link" in WordPress.

    https://codex.wordpress.org/Plugin_API/Filter_Reference/post_link

    So, let's say that you write such filter:

    add_filter( 'post_link', 'remove_specified_part_from_link', 10, 3 );
    function remove_specified_part_from_link( $permalink, $post, $leavename )
    {
    $permalink_array=explode("/",$permalink);
    unset($permalink_array[4]);
    return implode("/",$permalink_array);
    }
    

    When explode $permalink by "/", it will return "http:","","www.example.com","guide","africa","algeria","nome","ID". So we see that 5th element should be removed. That's why we unset $permalink_array[4] and return imploded array which would be equal to "http://www.example.com/guide/algeria/algeri/nome/ID"