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.

4 s
4

The easiest workaround could be:

function myplugin_update_slug( $data, $postarr ) {
    if ( ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
        $data['post_name'] = sanitize_title( $data['post_title'] );
    }

    return $data;
}
add_filter( 'wp_insert_post_data', 'myplugin_update_slug', 99, 2 );

Leave a Reply

Your email address will not be published. Required fields are marked *