wppaste
WordPress

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:5463
Computes a unique slug for the post, when given the desired slug and some post details.

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

Reaches the database via ->get_var().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
8–153

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 270.

Plugin surface
5 hooks

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.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()called directly
  • optionoption read or writeget_option()called directly
  • sqldatabase query->get_var()called directly
  • cacheobject cachewp_cache_get()one call below wp_unique_post_slug()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
always8–14none
!$post_status && $post_type !== "user_request" && $override_slug !== null24–26apply_filters()
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type !== "attachment" && is_post_type_hierarchical() && $post_type === "nav_menu_item"38–41apply_filters(), is_post_type_hierarchical()
!$post_status && $post_type !== "user_request" && $override_slug === null && $post_type === "attachment" && !$slug && $slug !== "embed"67–70apply_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–92apply_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–86apply_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–94apply_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–112apply_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–116apply_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–129apply_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" && $slug126–153apply_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

PHPCompiledExecutedBranchesNotes
8.6-dev2708–15338
8.52708–15338
8.42708–1533815 fewer instructions than PHP 8.3
8.32858–15938
8.22858–15938
8.12858–15938
7.42858–15938

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:

  1. apply_filters( pre_wp_unique_post_slug )filterline 5485 (+22 into the body)

    Filters the post slug before it is generated to be unique.

  2. apply_filters( wp_unique_post_slug_is_bad_attachment_slug )filterline 5512 (+49 into the body)

    Filters whether the post slug would make a bad attachment slug.

  3. apply_filters( wp_unique_post_slug_is_bad_hierarchical_slug )filterline 5548 (+85 into the body)

    Filters whether the post slug would make a bad hierarchical post slug.

  4. apply_filters( wp_unique_post_slug_is_bad_flat_slug )filterline 5604 (+141 into the body)

    Filters whether the post slug would be bad as a flat slug.

  5. apply_filters( wp_unique_post_slug )filterline 5633 (+170 into the body)

    Filters the unique post slug.

Uses · 5

Used by · 5

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.