I’m running a property site where many properties are sold in apartment blocks.
Because of this what the content editors do is create a post/property with all the details and then use a duplicate post plugin to create the others.
Each time they duplicate a post/property they change the title to reflect the property number and maybe change a few bits of meta data E.G price.
What they forget to do is wipe out the slug and let a new one be generated from the title. Here is an example slug from the first property entered:
merle-court-plot-50-182-carlton-vale-nw6-5hh
but then when they duplicate the slugs become:
merle-court-plot-50-182-carlton-vale-nw6-5hh-2
merle-court-plot-50-182-carlton-vale-nw6-5hh-2-2
merle-court-plot-50-182-carlton-vale-nw6-5hh-2-2-2
merle-court-plot-50-182-carlton-vale-nw6-5hh-2-2-2-2
etc
But when they change the titles the slugs would be better like:
merle-court-plot-51-182-carlton-vale-nw6-5hh
merle-court-plot-52-182-carlton-vale-nw6-5hh
merle-court-plot-53-182-carlton-vale-nw6-5hh
merle-court-plot-54-182-carlton-vale-nw6-5hh
etc
So my question:
How do I force the slug to be re-generated on post save, after they have updated the property title?
The slug for this CPT should always be auto generated, there is never a need to manually set it.
The easiest workaround could be:
Also, run the slug from
sanitize_title_with_dashes()
throughwp_unique_post_slug()
to ensure that it’s unique. It will automatically append ‘-2’, ‘-3’ etc. if it’s needed.Instead of replacing spaces you should use the build in function
sanitize_title()
which will take care of the replacing for you.Like this:
Also, you should use a unique slug. Which you can get with the function
wp_unique_post_slug()
So putting it all together a solution might be:
Something I’ve had bookedmarked for a little while is the following (yet untested),
Source LINK