How to append unique numbers to new duplicated post titles/urls?

As of now if you create two (2) posts with the same title you get the following:

domain.com/test/
domain.com/test-2/

Read More

If you create a third post, you then get another url like:

domain.com/test-3/

I am working on a directory, so the odds of someone having the same name is pretty good; therefore I want to append a custom url string to the end of the duplicated name – not just a 1 digit number.

Is there a way for me to add a custom 4-5 digit string at the end of a duplicate post/page/listing? So that it reads:

domain.com/test/
domain.com/test-#####/ < the random string

thanks!

Related posts

Leave a Reply

1 comment

  1. Filter wp_unique_post_slug. Make sure to add the callback with 6 as last argument to get the original slug. Then create a new unique slug as you need it, you get a lot of context information.

    add_filter(
        'wp_unique_post_slug',
        function (
            $slug,
            $post_ID,
            $post_status,
            $post_type,
            $post_parent,
            $original_slug
        )
    {
        // create a new unique slug based on $original_slug
        return $slug;
    }, 10, 6 );