wp_check_for_changed_slugs( int $post_id, WP_Post $post, WP_Post $post_before )
- Since
- 2.1.0
- Source
wp-includes/post.php:7567
Description
The function is used when a post object of any type is updated, by comparing the current and previous post objects.
If the slug was changed and not already part of the old slugs then it will be added to the post meta field ('_wp_old_slug') for storing old slugs for that post.
The most logically usage of this function is redirecting changed post objects, so that those that linked to an changed post will be redirected to the new post.
Compatibility
- WordPress
- since 2.1.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
$post_idint- Post ID.
$postWP_Post- The post object.
$post_beforeWP_Post- The previous post object.
Performance profile
How much work a call to wp_check_for_changed_slugs() 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
- Trivial
- Scaling
- Constant
- Instructions
- 8–52
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
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 54.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_check_for_changed_slugs() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–17 | none |
is_post_type_hierarchical() | 17–23 | is_post_type_hierarchical() |
!is_post_type_hierarchical() && !$old_slugs | 28–38 | is_post_type_hierarchical(), get_post_meta() |
!is_post_type_hierarchical() && $old_slugs | 35–45 | is_post_type_hierarchical(), get_post_meta(), delete_post_meta() |
!is_post_type_hierarchical() && !empty($post_before) && !$old_slugs | 39–45 | is_post_type_hierarchical(), get_post_meta(), add_post_meta() |
!is_post_type_hierarchical() && !empty($post_before) | 46–52 | is_post_type_hierarchical(), get_post_meta(), add_post_meta(), delete_post_meta() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 54 | 8–52 | 8 | |
| 8.5 | 54 | 8–52 | 8 | |
| 8.4 | 54 | 8–52 | 8 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 60 | 8–58 | 8 | |
| 8.2 | 60 | 8–58 | 8 | |
| 8.1 | 60 | 8–58 | 8 | |
| 7.4 | 60 | 8–58 | 8 |
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 · 4
- is_post_type_hierarchical()Determines whether the post type is hierarchical.
- get_post_meta()Retrieves a post meta field for the given post ID.
- add_post_meta()Adds a meta field to the given post.
- delete_post_meta()Deletes a post meta field for the given post ID.
Source code
function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { // Don't bother if it hasn't changed. if ( $post->post_name === $post_before->post_name ) { return; } // We're only concerned with published, non-hierarchical objects. if ( ! ( 'publish' === $post->post_status || ( 'attachment' === $post->post_type && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) { return; } $old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' ); // If we haven't added this old slug before, add it now. if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs, true ) ) { add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name ); } // If the new slug was used previously, delete it from the list. if ( in_array( $post->post_name, $old_slugs, true ) ) { delete_post_meta( $post_id, '_wp_old_slug', $post->post_name ); }}Changelog
Introduced in 2.1.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 7.1.0 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.