Replace WordPress %postname% suffix with %postid%?

I suppose you could filter wp_unique_post_slug and have it return the original slug for posts, but I’m not sure what kind of side-effects this may have:

add_filter( 'wp_unique_post_slug', function( $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ) {
    if ( $post_type == 'post' )
        $slug = $original_slug;

    return $slug;
}, 10, 6 );

And here’s how you would append the post ID instead of -2:

add_filter( 'wp_unique_post_slug', function( $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ) {
    if ( $post_type == 'post' && $slug != $original_slug )
        $slug = preg_replace( '#\-[0-9]+$#', '-' . $post_id, $slug );

    return $slug;
}, 10, 6 );

Leave a Comment