Using WordPress ‘pre_wp_unique_post_slug’ PHP filter

The pre_wp_unique_post_slug WordPress PHP filter is used to Filters the post slug before it is generated.

This allows you to override the default post slug generation and create a custom slug instead.

Usage

add_filter( 'pre_wp_unique_post_slug', 'my_custom_post_slug', 10, 6 );

function my_custom_post_slug( $override_slug, $slug, $post_id, $post_status, $post_type, $post_parent ) {
    // Your custom slug logic here
}

Parameters

  • $override_slug (string|null) – Short-circuit return value.
  • $slug (string) – The desired slug (post_name).
  • $post_id (int) – Post ID.
  • $post_status (string) – The post status.
  • $post_type (string) – Post type.
  • $post_parent (int) – Post parent ID.

Examples

Adding a prefix to post slugs

add_filter( 'pre_wp_unique_post_slug', 'add_prefix_to_slug', 10, 6 );

function add_prefix_to_slug( $override_slug, $slug, $post_id, $post_status, $post_type, $post_parent ) {
    if ( 'post' === $post_type ) {
        return 'myprefix-' . $slug;
    }
    return $override_slug;
}

This example adds a custom prefix “myprefix-” to the post slug for all posts. The add_prefix_to_slug() function checks if the post type is ‘post’ and then adds the prefix to the slug.

Using post ID as the slug

add_filter( 'pre_wp_unique_post_slug', 'use_post_id_as_slug', 10, 6 );

function use_post_id_as_slug( $override_slug, $slug, $post_id, $post_status, $post_type, $post_parent ) {
    if ( 'post' === $post_type ) {
        return (string) $post_id;
    }
    return $override_slug;
}

This example replaces the post slug with the post ID for all posts. The use_post_id_as_slug() function checks if the post type is ‘post’ and then returns the post ID as the slug.

Adding a custom suffix to post slugs

add_filter( 'pre_wp_unique_post_slug', 'add_suffix_to_slug', 10, 6 );

function add_suffix_to_slug( $override_slug, $slug, $post_id, $post_status, $post_type, $post_parent ) {
    if ( 'post' === $post_type ) {
        return $slug . '-customsuffix';
    }
    return $override_slug;
}

This example adds a custom suffix “-customsuffix” to the post slug for all posts. The add_suffix_to_slug() function checks if the post type is ‘post’ and then appends the suffix to the slug.

Adding a date to post slugs

add_filter( 'pre_wp_unique_post_slug', 'add_date_to_slug', 10, 6 );

function add_date_to_slug( $override_slug, $slug, $post_id, $post_status, $post_type, $post_parent ) {
    if ( 'post' === $post_type ) {
        $date = get_the_date( 'Y-m-d', $post_id );
        return $slug . '-' . $date;
    }
    return $override_slug;
}

This example appends the post’s publish date to the post slug for all posts. The add_date_to_slug() function checks if the post type is ‘post’ and then retrieves the post’s publish date using get_the_date() function. The date is then appended to the slug.

Changing slugs for custom post types

add_filter( 'pre_wp_unique_post_slug', 'change_custom_post_type_slug', 10, 6 );

function change_custom_post_type_slug( $override_slug, $slug, $post_id, $post_status, $post_type, $post_parent ) {
    if ( 'my_custom_post_type' === $post_type ) {
        return 'custom-' . $slug;
    }
    return $override_slug;
}

This example adds a custom prefix “custom-” to the post slug for all posts with the custom post type ‘my_custom_post_type’. The change_custom_post_type_slug() function checks if the post type is ‘my_custom_post_type’ and then adds the prefix to the slug.