Title: _find_post_by_old_slug
Published: October 5, 2018
Last modified: August 20, 2026

---

# _find_post_by_old_slug( string $post_type ): int

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#see-also)
 * [Parameters](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Find the post ID for redirecting an old slug.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#description)󠁿

### 󠀁[See also](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#see-also)󠁿

 * [wp_old_slug_redirect()](https://developer.wordpress.org/reference/functions/wp_old_slug_redirect/)

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#parameters)󠁿

 `$post_type`stringrequired

The current post type based on the query vars.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#return)󠁿

 int The Post ID.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#source)󠁿

    ```php
    function _find_post_by_old_slug( $post_type ) {
    	global $wpdb;

    	$query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, get_query_var( 'name' ) );

    	/*
    	 * If year, monthnum, or day have been specified, make our query more precise
    	 * just in case there are multiple identical _wp_old_slug values.
    	 */
    	if ( get_query_var( 'year' ) ) {
    		$query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
    	}
    	if ( get_query_var( 'monthnum' ) ) {
    		$query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
    	}
    	if ( get_query_var( 'day' ) ) {
    		$query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
    	}

    	$key          = md5( $query );
    	$last_changed = wp_cache_get_last_changed( 'posts' );
    	$cache_key    = "find_post_by_old_slug:$key";
    	$cache        = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );
    	if ( false !== $cache ) {
    		$id = $cache;
    	} else {
    		$id = (int) $wpdb->get_var( $query );
    		wp_cache_set_salted( $cache_key, $id, 'post-queries', $last_changed );
    	}

    	return $id;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/query.php#L1157)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/query.php#L1157-L1188)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_cache_get_salted()](https://developer.wordpress.org/reference/functions/wp_cache_get_salted/)`wp-includes/cache-compat.php` |

Retrieves cached data if valid and unchanged.

  | 
| [wp_cache_set_salted()](https://developer.wordpress.org/reference/functions/wp_cache_set_salted/)`wp-includes/cache-compat.php` |

Stores salted data in the cache.

  | 
| [wp_cache_get_last_changed()](https://developer.wordpress.org/reference/functions/wp_cache_get_last_changed/)`wp-includes/functions.php` |

Gets last changed date for the specified cache group.

  | 
| [get_query_var()](https://developer.wordpress.org/reference/functions/get_query_var/)`wp-includes/query.php` |

Retrieves the value of a query variable in the [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/) class.

  | 
| [wpdb::get_var()](https://developer.wordpress.org/reference/classes/wpdb/get_var/)`wp-includes/class-wpdb.php` |

Retrieves one value from the database.

  | 
| [wpdb::prepare()](https://developer.wordpress.org/reference/classes/wpdb/prepare/)`wp-includes/class-wpdb.php` |

Prepares a SQL query for safe execution.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#)

| Used by | Description | 
| [wp_old_slug_redirect()](https://developer.wordpress.org/reference/functions/wp_old_slug_redirect/)`wp-includes/query.php` |

Redirect old slugs to the correct permalink.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.9.3](https://developer.wordpress.org/reference/since/4.9.3/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_find_post_by_old_slug%2F)
before being able to contribute a note or feedback.