wp_resolve_numeric_slug_conflicts( array $query_vars = array() ): array
- Since
- 4.3.0
- Source
wp-includes/rewrite.php:381
Description
Permalinks of posts with numeric slugs can sometimes look to WP_Query::parse_query() like a date archive, as when your permalink structure is /%year%/%postname%/ and a post with post_name '05' has the URL /2015/05/.
This function detects conflicts of this type and resolves them in favor of the post permalink.
Note that, since 4.3.0, wp_unique_post_slug() prevents the creation of post slugs that would result in a date archive conflict. The resolution performed in this function is primarily for legacy content, as well as cases when the admin has changed the site's permalink structure in a way that introduces URL conflicts.
Compatibility
- WordPress
- since 4.3.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
$query_varsarrayoptional- Query variables for setting up the loop, as determined in WP::parse_request(). Default empty array.Default:
array()
Return value
array- Returns the original array of query vars, with date/post conflicts resolved.
Performance profile
How much work a call to wp_resolve_numeric_slug_conflicts() 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
- Constant
- Instructions
- 31–116
- Plugin surface
- None
- Called by
- 2
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 152.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()one call below wp_resolve_numeric_slug_conflicts() - cacheobject cache
wp_cache_get()one call below wp_resolve_numeric_slug_conflicts() - serializeserialisation
maybe_unserialize()one call below wp_resolve_numeric_slug_conflicts() - querycontent query
get_post()one call below wp_resolve_numeric_slug_conflicts()
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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_resolve_numeric_slug_conflicts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
isset($query_vars) && $postname_index !== false && $postname_index !== 0 | 31 | get_option(), explode(), array_filter(), array_values(), array_search() |
isset($query_vars) && $postname_index !== false && $postname_index !== 0 && !($post instanceof) | 42–49 | get_option(), explode(), array_filter(), array_values(), array_search(), get_page_by_path() |
isset($query_vars) && $postname_index !== false && $postname_index !== 0 && $post instanceof && preg_match() | 59–78 | get_option(), explode(), array_filter(), array_values(), array_search(), get_page_by_path(), preg_match() |
isset($query_vars) && $postname_index !== false && $postname_index !== 0 && $post instanceof | 66–116 | get_option(), explode(), array_filter(), array_values(), array_search(), get_page_by_path(), preg_match(), substr_count() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 152 | 31–116 | 35 | |
| 8.5 | 152 | 31–116 | 35 | |
| 8.4 | 152 | 31–116 | 35 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 155 | 31–119 | 35 | |
| 8.2 | 155 | 31–119 | 35 | |
| 8.1 | 155 | 31–119 | 35 | |
| 7.4 | 155 | 31–119 | 35 |
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.
Uses · 2
- get_option()Retrieves an option value based on an option name.
- get_page_by_path()Retrieves a page given its path.
Used by · 2
- WP::parse_request()Parses the request to find the correct WordPress query.
- url_to_postid()Examines a URL and try to determine the post ID it represents.
Source code
function wp_resolve_numeric_slug_conflicts( $query_vars = array() ) { if ( ! isset( $query_vars['year'] ) && ! isset( $query_vars['monthnum'] ) && ! isset( $query_vars['day'] ) ) { return $query_vars; } // Identify the 'postname' position in the permastruct array. $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); $postname_index = array_search( '%postname%', $permastructs, true ); if ( false === $postname_index ) { return $query_vars; } /* * A numeric slug could be confused with a year, month, or day, depending on position. To account for * the possibility of post pagination (eg 2015/2 for the second page of a post called '2015'), our * `is_*` checks are generous: check for year-slug clashes when `is_year` *or* `is_month`, and check * for month-slug clashes when `is_month` *or* `is_day`. */ $compare = ''; if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset( $query_vars['monthnum'] ) ) ) { $compare = 'year'; } elseif ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && ( isset( $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) { $compare = 'monthnum'; } elseif ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && isset( $query_vars['day'] ) ) { $compare = 'day'; } if ( ! $compare ) { return $query_vars; } // This is the potentially clashing slug. $value = ''; if ( $compare && array_key_exists( $compare, $query_vars ) ) { $value = $query_vars[ $compare ]; } $post = get_page_by_path( $value, OBJECT, 'post' ); if ( ! ( $post instanceof WP_Post ) ) { return $query_vars; } // If the date of the post doesn't match the date specified in the URL, resolve to the date archive. if ( preg_match( '/^([0-9]{4})\-([0-9]{2})/', $post->post_date, $matches ) && isset( $query_vars['year'] ) && ( 'monthnum' === $compare || 'day' === $compare ) ) { // $matches[1] is the year the post was published. if ( (int) $query_vars['year'] !== (int) $matches[1] ) { return $query_vars; } // $matches[2] is the month the post was published. if ( 'day' === $compare && isset( $query_vars['monthnum'] ) && (int) $query_vars['monthnum'] !== (int) $matches[2] ) { return $query_vars; } } /* * If the located post contains nextpage pagination, then the URL chunk following postname may be * intended as the page number. Verify that it's a valid page before resolving to it. */ $maybe_page = ''; if ( 'year' === $compare && isset( $query_vars['monthnum'] ) ) { $maybe_page = $query_vars['monthnum']; } elseif ( 'monthnum' === $compare && isset( $query_vars['day'] ) ) { $maybe_page = $query_vars['day']; } // Bug found in #11694 - 'page' was returning '/4'. $maybe_page = (int) trim( $maybe_page, '/' ); $post_page_count = substr_count( $post->post_content, '<!--nextpage-->' ) + 1; // If the post doesn't have multiple pages, but a 'page' candidate is found, resolve to the date archive. if ( 1 === $post_page_count && $maybe_page ) { return $query_vars; } // If the post has multiple pages and the 'page' number isn't valid, resolve to the date archive. if ( $post_page_count > 1 && $maybe_page > $post_page_count ) { return $query_vars; }Changelog
Introduced in 4.3.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.8.8 tag, from
src/wp-includes/rewrite.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.