Why does WordPress Append -2 to any Numeric Title Page?

Hey Guys,

A customer brought to my attention a very strange issue that I never noticed within WordPress and I didn't have an answer other than its default WordPress.

Does anyone know why WordPress automatically appends "-2" to any page that gets created with a numeric title and slug? This is not an issue with trashed pages not being deleted.

It literally happens to any page that you create with a numeric title like "593935". The slug automatically gets -2 added to it. I noticed this doesn't happen with posts, just for pages. Any ideas? If so, how about a way to avoid this? Thanks everyone!

  • Tyler Postle
    • CGO

    Hey Mike,

    Hope you're doing well today and thanks for your question!

    I have never noticed that before, I don't think i have ever created a pure number page title until now :p

    I see what you mean though and to be completely honest, I'm not sure. I can't think of a good reason for core WordPress to do this, but I'm thinking there must be one.

    This is what usually happens if the page already exists as you alluded too.

    Are you able to add 1 letter to it? If you edit the permalink and add even just 1 letter at the beginning then it will avoid it :slight_smile:

    Look forward to hearing back!

    Cheers,
    Tyler

  • Mike Price
    • The Incredible Code Injector

    Hey Tyler,

    Thanks for the response. For some reason, the customer was really adamant about wanting just a number URL so they didn't want to use the workaround you mentioned.

    The solution I gave them was to simply set up a redirect from their desired URL to the one WordPress is auto creating using our Nginx Redirect Rules. He says he would do that but my curiosity led me to want to know WHY this is happening LOL

    Would love to know if anyone here might know the reason. Thanks again!

  • aristath
    • Recruit

    Hello again @Mike Price,

    Ha! That's really weird, I never noticed that!

    My guess is it adds these to avoid confusion with post IDs.
    I mean can you imagine having a page with id=3 and slug=5 ?
    If you wanted to create a custom template file and you named it page-5.php, would that be page with id=5, or slug=5?
    At least with the -2 it adds, it can differentiate.

    I'm not saying that's why they did it, but if I were a WordPress core dev, that's what I'd do to avoid the issue I mentioned above.

    I hope that makes sense!

    Cheers,
    Ari.

  • Mike Price
    • The Incredible Code Injector

    Hey Ari,

    Thanks for your response. I thought the same thing but then makes me curious as to why this same thing isn't mirrored on posts ? You can create a post with a numeric title and no "-2" is added to the slug. Since pages are just a post type, you would think this would happen on both.

    Very interesting though!

  • aristath
    • Recruit

    Hello again @Mike Price, I hope you're well today!

    I did some research and found out why this only happens with pages...
    This is actually a 7-year old ticket on WordPress trac: https://core.trac.wordpress.org/ticket/5305
    The problem is that pages have pagination.
    There used to be a plugin that allowed numeric URLs for pages: https://wordpress.org/plugins/allow-numeric-stubs/
    It hasn't been updated in a loooong time, but it should be fairly simple bringing it up to speed with latest WordPress core so it's compatible. :slight_smile:

    Cheers,
    Ari.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.