Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness. Use wp_old_slug_redirect() instead.

_find_post_by_old_slug() WordPress Function

The find_post_by_old_slug() function allows you to retrieve a post by its old slug. This is useful if you have changed the slug for a post and need to redirect users from the old slug to the new one.

_find_post_by_old_slug( string $post_type ) #

Find the post ID for redirecting an old slug.


Description

Top ↑

See also


Top ↑

Parameters

$post_type

(string)(Required)The current post type based on the query vars.


Top ↑

Return

(int) The Post ID.


Top ↑

Source

File: wp-includes/query.php

1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
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' ) );
    }
 
    $id = (int) $wpdb->get_var( $query );
 
    return $id;
}


Top ↑

Changelog

Changelog
VersionDescription
4.9.3Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.