wp_unique_post_slug( string $slug, int $post_id, string $post_status, string $post_type, int $post_parent ): string
- Since
- 2.8.0
- Source
wp-includes/post.php:5464
Compatibility
- WordPress
- since 2.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$slugstring- The desired slug (post_name).
$post_idint- Post ID.
$post_statusstring- No uniqueness checks are made if the post is still draft or pending.
$post_typestring- Post type.
$post_parentint- Post parent ID.
Return value
string- Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
Performance profile
How much work a call to wp_unique_post_slug() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 8–153
- Plugin surface
- 5 hooks
- Called by
- 5
Reaches the database via ->get_var().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 270.
Third-party callbacks on 'pre_wp_unique_post_slug', 'wp_unique_post_slug_is_bad_attachment_slug', 'wp_unique_post_slug_is_bad_hierarchical_slug' run inside this call, and their cost is not bounded by anything here.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_post()called directly - optionoption read or write
get_option()called directly - sqldatabase query
->get_var()called directly - cacheobject cache
wp_cache_get()one call below wp_unique_post_slug() - serializeserialisation
maybe_unserialize()one call below wp_unique_post_slug()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 11 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_unique_post_slug() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–14 | none |
!$post_status && $post_type !== "user_request" && $override_slug !== null | 24–26 | apply_filters() |
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type !== "attachment" && is_post_type_hierarchical() && $post_type === "nav_menu_item" | 38–41 | apply_filters(), is_post_type_hierarchical() |
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type === "attachment" && !$slug && $slug !== "embed" | 67–70 | apply_filters(), ->prepare(), ->get_var(), apply_filters(), apply_filters() |
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type !== "attachment" && !is_post_type_hierarchical() && !$slug && $slug !== "embed" | 81–92 | apply_filters(), is_post_type_hierarchical(), ->prepare(), ->get_var(), get_post(), apply_filters(), apply_filters() |
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type !== "attachment" && is_post_type_hierarchical() && $post_type !== "nav_menu_item" && !$slug && $slug !== "embed" | 83–86 | apply_filters(), is_post_type_hierarchical(), ->prepare(), ->get_var(), apply_filters(), apply_filters() |
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type === "attachment" | 85–94 | apply_filters(), ->prepare(), ->get_var(), apply_filters(), _truncate_post_slug(), ->prepare(), ->get_var(), apply_filters() |
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type !== "attachment" && is_post_type_hierarchical() && $post_type !== "nav_menu_item" | 97–112 | apply_filters(), is_post_type_hierarchical(), ->prepare(), ->get_var(), apply_filters(), _truncate_post_slug(), ->prepare(), ->get_var(), apply_filters() |
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type !== "attachment" && !is_post_type_hierarchical() | 98–116 | apply_filters(), is_post_type_hierarchical(), ->prepare(), ->get_var(), get_post(), apply_filters(), _truncate_post_slug(), ->prepare(), ->get_var(), apply_filters() |
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type !== "attachment" && !is_post_type_hierarchical() && $post_type === "post" && $slug !== "embed" | 109–129 | apply_filters(), is_post_type_hierarchical(), ->prepare(), ->get_var(), get_post(), get_option(), explode(), array_filter(), array_values(), array_search(), apply_filters(), apply_filters() |
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type !== "attachment" && !is_post_type_hierarchical() && $post_type === "post" && $slug | 126–153 | apply_filters(), is_post_type_hierarchical(), ->prepare(), ->get_var(), get_post(), get_option(), explode(), array_filter(), array_values(), array_search(), apply_filters(), _truncate_post_slug(), ->prepare(), ->get_var(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 270 | 8–153 | 38 | |
| 8.5 | 270 | 8–153 | 38 | |
| 8.4 | 270 | 8–153 | 38 | 15 fewer instructions than PHP 8.3 |
| 8.3 | 285 | 8–159 | 38 | |
| 8.2 | 285 | 8–159 | 38 | |
| 8.1 | 285 | 8–159 | 38 | |
| 7.4 | 285 | 8–159 | 38 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 5
5 hooks fire while wp_unique_post_slug() runs, in this order:
- apply_filters( pre_wp_unique_post_slug )filterline 5486 (+22 into the body)
Filters the post slug before it is generated to be unique.
- apply_filters( wp_unique_post_slug_is_bad_attachment_slug )filterline 5513 (+49 into the body)
Filters whether the post slug would make a bad attachment slug.
- apply_filters( wp_unique_post_slug_is_bad_hierarchical_slug )filterline 5549 (+85 into the body)
Filters whether the post slug would make a bad hierarchical post slug.
- apply_filters( wp_unique_post_slug_is_bad_flat_slug )filterline 5605 (+141 into the body)
Filters whether the post slug would be bad as a flat slug.
- apply_filters( wp_unique_post_slug )filterline 5634 (+170 into the body)
Filters the unique post slug.
Uses · 5
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _truncate_post_slug()Truncates a post slug.
- is_post_type_hierarchical()Determines whether the post type is hierarchical.
- get_post()Retrieves post data given a post ID or post object.
- get_option()Retrieves an option value based on an option name.
Used by · 5
- WP_REST_Posts_Controller::create_item()Creates a single post.
- WP_REST_Posts_Controller::update_item()Updates a single post.
- get_sample_permalink()Returns a sample permalink based on the post name.
- wp_ajax_inline_save()Handles Quick Edit saving a post from a list table via AJAX.
- wp_insert_post()Inserts or update a post.
Source code
function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_parent ) { if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) || ( 'inherit' === $post_status && 'revision' === $post_type ) || 'user_request' === $post_type ) { return $slug; } /** * Filters the post slug before it is generated to be unique. * * Returning a non-null value will short-circuit the * unique slug generation, returning the passed value instead. * * @since 5.1.0 * * @param string|null $override_slug Short-circuit return value. * @param string $slug The desired slug (post_name). * @param int $post_id Post ID. * @param string $post_status The post status. * @param string $post_type Post type. * @param int $post_parent Post parent ID. */ $override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_id, $post_status, $post_type, $post_parent ); if ( null !== $override_slug ) { return $override_slug; } global $wpdb, $wp_rewrite; $original_slug = $slug; $feeds = $wp_rewrite->feeds; if ( ! is_array( $feeds ) ) { $feeds = array(); } if ( 'attachment' === $post_type ) { // Attachment slugs must be unique across all types. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_id ) ); /** * Filters whether the post slug would make a bad attachment slug. * * @since 3.1.0 * * @param bool $bad_slug Whether the slug would be bad as an attachment slug. * @param string $slug The post slug. */ $is_bad_attachment_slug = apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ); if ( $post_name_check || in_array( $slug, $feeds, true ) || 'embed' === $slug || $is_bad_attachment_slug ) { $suffix = 2; do { $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_id ) ); ++$suffix; } while ( $post_name_check ); $slug = $alt_post_name; } } elseif ( is_post_type_hierarchical( $post_type ) ) { if ( 'nav_menu_item' === $post_type ) { return $slug; } /* * Page slugs must be unique within their own trees. Pages are in a separate * namespace than posts so page slugs are allowed to overlap post slugs. */ $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_id, $post_parent ) ); /** * Filters whether the post slug would make a bad hierarchical post slug. * * @since 3.1.0 *Changelog
Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.