Removing unnecessary numbers appended to postname in permalinks

I have, with help from Milo managed to tweak the permalinks for my custom post type so that they include the year and month of the event. e.g.

  • mysite.com/events/2013/january/fishing-trip
  • mysite.com/events/2013/may/picnic
  • mysite.com/events/2013/may/visit-grandma

One small issue still evades me. If there is more than one event with the same name, WordPress appends a number to the end of the name even when the month is different and so there is no need for the name to be unique.

Read More

In other words I get

  • mysite.com/events/2013/january/fishing-trip
  • mysite.com/events/2013/may/picnic
  • mysite.com/events/2013/june/fishing-trip-2

when I want

  • mysite.com/events/2013/january/fishing-trip
  • mysite.com/events/2013/may/picnic
  • mysite.com/events/2013/june/fishing-trip

Is there a solution for this? I’ve got permalinks set to “postname” in permalink settings if this matters.

Related posts

Leave a Reply

1 comment

  1. There is a plugin for wordpress which allows you to handle 404 errors and redirect to specific pages. So if wordpress calls your post /events/2013/june/fishing-trip-2, you could go into the plugin and add a rule so that /events/2013/june/fishing-trip points to /events/2013/june/fishing-trip-2.

    It’s not a perfect solution, as you’d have to manually enter each duplicate post name to the rewrite rules, and even then the links wordpress generates for that post would still have the -2. You could write a function using preg_replace to then remove any -2 or -3 or whatever from the end of a link, but it’s a little messy