Had a very frustrating couple of days fighting WordPress, so I thought I’d share my findings in the hope that someone else may benefit. The slugs incidentally are the part that form the permalink of the page/post – so this post’s slug will be 2022/01/14/wordpress-slug-autocorrect, leading to a permalink of https://www.eutony.net/2022/01/14/wordpress-slug-autocorrect

Rather than spin the whole narrative, I’m going to just say upfront – WordPress does not allow you to have pure numeric slugs (e.g. “365” or “123”), and if you try to do this it will ‘autocorrect’ to add a “-2” on the end. Simple as that.

In trying to chase this down, it turns out that there is a whole bunch of other reasons why it also might add a “-2” on the end. These include if there is:

  • a media item with the slug name,
  • a deleted item with that slug name in the bin,
  • a tag or category of that name, or
  • an old slug hanging around in wp_postmeta (the metakey being _wp_old_slug).

My problem was this – I have a templated page which is a sort of summary of my 365 photo project, and shows the most recent 10 photos. [It does this with a bit of PHP which hits the API at photo.eutony.net and generates a page of img tags and links to that site]. Naturally enough I wanted this to be at www.eutony.net/365. Not unreasonable, I wouldn’t have thought, and it worked when I first did it.

Anyway, I fairly recently updated WordPress and PHP, and all seemed to be well. However I noticed on the front page that my 365 photo wasn’t updating. Turns out there were a bunch of missing PHP modules (such as curl) that meant the backend caching had broken. So I eventually fixed that, and then noticed that my /365 page had turned itself into /365-2.

Even worse, navigating to /365 took me to a fairly random one of my blog posts where the title started “365-“!!

I nailed this last problem first – turns out there is some wordpress magic called “template_redirect” which does some canonical SEO stuff, but also guesses what page you might be looking for! In WordPress 5.5 the ability to turn this off was added (make.wordpress.org/core/2020/06/26/wordpress-5-5-better-fine-grained-control-of-redirect_guess_404_permalink) – upshot is that you need the following line in your theme’s function.php

add_filter( 'do_redirect_guess_404_permalink', '__return_false' );

Once I discovered about the WordPress numeric slug limitation, answer was to rename the slug to “/365-photos”, and then add a couple of PermanentRedirect to .htaccess to preserve the previous published permalink(s):

RedirectPermanent /365 /365-photos
RedirectPermanent /365-2 /365-photos

As often with computing and software, the solution, once you know what it is, is a two minute job to implement – it’s getting to it that takes hours.