wp_old_slug_redirect

The timeline below displays how wordpress function wp_old_slug_redirect has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.4

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 */
function wp_old_slug_redirect()
{
    if (is_404() && '' !== get_query_var('name')) {
        // Guess the current post type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (get_query_var('pagename')) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types.
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $id = _find_post_by_old_slug($post_type);
        if (!$id) {
            $id = _find_post_by_old_date($post_type);
        }
        /**
         * Filters the old slug redirect post ID.
         *
         * @since 4.9.3
         *
         * @param int $id The redirect post ID.
         */
        $id = apply_filters('old_slug_redirect_post_id', $id);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (get_query_var('paged') > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . get_query_var('paged'));
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        }
        /**
         * Filters the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect.
        exit;
    }
}

WordPress Version: 9.6

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 */
function wp_old_slug_redirect()
{
    if (is_404() && '' !== get_query_var('name')) {
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (get_query_var('pagename')) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $id = _find_post_by_old_slug($post_type);
        if (!$id) {
            $id = _find_post_by_old_date($post_type);
        }
        /**
         * Filters the old slug redirect post ID.
         *
         * @since 4.9.3
         *
         * @param int $id The redirect post ID.
         */
        $id = apply_filters('old_slug_redirect_post_id', $id);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (get_query_var('paged') > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . get_query_var('paged'));
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        }
        /**
         * Filters the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: 9.3

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 */
function wp_old_slug_redirect()
{
    if (is_404() && '' !== get_query_var('name')) {
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (get_query_var('pagename')) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $id = _find_post_by_old_slug($post_type);
        if (!$id) {
            $id = _find_post_by_old_date($post_type);
        }
        /**
         * Filters the old slug redirect post ID.
         *
         * @since 4.9.3
         *
         * @param string $id The redirect post ID.
         */
        $id = apply_filters('old_slug_redirect_post_id', $id);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (get_query_var('paged') > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . get_query_var('paged'));
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        }
        /**
         * Filters the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: .20

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 */
function wp_old_slug_redirect()
{
    if (is_404() && '' !== get_query_var('name')) {
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (get_query_var('pagename')) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $id = _find_post_by_old_slug($post_type);
        if (!$id) {
            $id = _find_post_by_old_date($post_type);
        }
        /**
         * Filters the old slug redirect post ID.
         *
         * @since 4.9.3
         *
         * @param int $id The redirect post ID.
         */
        $id = apply_filters('old_slug_redirect_post_id', $id);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (get_query_var('paged') > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . get_query_var('paged'));
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        }
        /**
         * Filters the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: 9.2

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 */
function wp_old_slug_redirect()
{
    if (is_404() && '' !== get_query_var('name')) {
        global $wpdb;
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (get_query_var('pagename')) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $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);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (get_query_var('paged') > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . get_query_var('paged'));
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        }
        /**
         * Filters the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: .10

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 */
function wp_old_slug_redirect()
{
    if (is_404() && '' !== get_query_var('name')) {
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (get_query_var('pagename')) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $id = _find_post_by_old_slug($post_type);
        if (!$id) {
            $id = _find_post_by_old_date($post_type);
        }
        /**
         * Filters the old slug redirect post ID.
         *
         * @since 4.9.3
         *
         * @param int $id The redirect post ID.
         */
        $id = apply_filters('old_slug_redirect_post_id', $id);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (get_query_var('paged') > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . get_query_var('paged'));
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        }
        /**
         * Filters the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: 4.7

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 */
function wp_old_slug_redirect()
{
    if (is_404() && '' !== get_query_var('name')) {
        global $wpdb;
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (get_query_var('pagename')) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $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);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (get_query_var('paged') > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . get_query_var('paged'));
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        }
        /**
         * Filters the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: 4.6

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 *
 * @global WP_Query   $wp_query   Global WP_Query instance.
 * @global wpdb       $wpdb       WordPress database abstraction object.
 */
function wp_old_slug_redirect()
{
    global $wp_query;
    if (is_404() && '' !== $wp_query->query_vars['name']) {
        global $wpdb;
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (!empty($wp_query->query_vars['pagename'])) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $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, $wp_query->query_vars['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 ('' != $wp_query->query_vars['year']) {
            $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
        }
        if ('' != $wp_query->query_vars['monthnum']) {
            $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
        }
        if ('' != $wp_query->query_vars['day']) {
            $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
        }
        $id = (int) $wpdb->get_var($query);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (isset($GLOBALS['wp_query']->query_vars['paged']) && $GLOBALS['wp_query']->query_vars['paged'] > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . $GLOBALS['wp_query']->query_vars['paged']);
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        }
        /**
         * Filters the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: .10

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 *
 * @global WP_Query   $wp_query   Global WP_Query instance.
 * @global wpdb       $wpdb       WordPress database abstraction object.
 */
function wp_old_slug_redirect()
{
    global $wp_query;
    if (is_404() && '' !== $wp_query->query_vars['name']) {
        global $wpdb;
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (!empty($wp_query->query_vars['pagename'])) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $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, $wp_query->query_vars['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 ('' != $wp_query->query_vars['year']) {
            $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
        }
        if ('' != $wp_query->query_vars['monthnum']) {
            $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
        }
        if ('' != $wp_query->query_vars['day']) {
            $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
        }
        $id = (int) $wpdb->get_var($query);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (isset($GLOBALS['wp_query']->query_vars['paged']) && $GLOBALS['wp_query']->query_vars['paged'] > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . $GLOBALS['wp_query']->query_vars['paged']);
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        }
        /**
         * Filter the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: 4.1

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 *
 * @global WP_Query   $wp_query   Global WP_Query instance.
 * @global wpdb       $wpdb       WordPress database abstraction object.
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 */
function wp_old_slug_redirect()
{
    global $wp_query, $wp_rewrite;
    if (get_queried_object()) {
        return;
    }
    if ('' !== $wp_query->query_vars['name']) {
        global $wpdb;
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (!empty($wp_query->query_vars['pagename'])) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $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, $wp_query->query_vars['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 ('' != $wp_query->query_vars['year']) {
            $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
        }
        if ('' != $wp_query->query_vars['monthnum']) {
            $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
        }
        if ('' != $wp_query->query_vars['day']) {
            $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
        }
        $id = (int) $wpdb->get_var($query);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (is_feed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'feed');
        } elseif (isset($GLOBALS['wp_query']->query_vars['paged']) && $GLOBALS['wp_query']->query_vars['paged'] > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . $GLOBALS['wp_query']->query_vars['paged']);
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        } elseif (is_404()) {
            // Add rewrite endpoints if necessary.
            foreach ($wp_rewrite->endpoints as $endpoint) {
                if ($endpoint[2] && false !== get_query_var($endpoint[2], false)) {
                    $link = user_trailingslashit(trailingslashit($link) . $endpoint[1]);
                }
            }
        }
        /**
         * Filter the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: 4.4

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 *
 * @global WP_Query   $wp_query   Global WP_Query instance.
 * @global wpdb       $wpdb       WordPress database abstraction object.
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 */
function wp_old_slug_redirect()
{
    global $wp_query, $wp_rewrite;
    if ('' !== $wp_query->query_vars['name']) {
        global $wpdb;
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (get_query_var('attachment')) {
            $post_type = 'attachment';
        } elseif (!empty($wp_query->query_vars['pagename'])) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $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, $wp_query->query_vars['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 ('' != $wp_query->query_vars['year']) {
            $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
        }
        if ('' != $wp_query->query_vars['monthnum']) {
            $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
        }
        if ('' != $wp_query->query_vars['day']) {
            $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
        }
        $id = (int) $wpdb->get_var($query);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (is_feed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'feed');
        } elseif (isset($GLOBALS['wp_query']->query_vars['paged']) && $GLOBALS['wp_query']->query_vars['paged'] > 1) {
            $link = user_trailingslashit(trailingslashit($link) . 'page/' . $GLOBALS['wp_query']->query_vars['paged']);
        } elseif (is_embed()) {
            $link = user_trailingslashit(trailingslashit($link) . 'embed');
        } elseif (is_404()) {
            // Add rewrite endpoints if necessary.
            foreach ($wp_rewrite->endpoints as $endpoint) {
                if ($endpoint[2] && false !== get_query_var($endpoint[2], false)) {
                    $link = user_trailingslashit(trailingslashit($link) . $endpoint[1]);
                }
            }
        }
        /**
         * Filter the old slug redirect URL.
         *
         * @since 4.4.0
         *
         * @param string $link The redirect URL.
         */
        $link = apply_filters('old_slug_redirect_url', $link);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: 4.3

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 *
 * @global WP_Query $wp_query
 * @global wpdb     $wpdb     WordPress database abstraction object.
 */
function wp_old_slug_redirect()
{
    global $wp_query;
    if (is_404() && '' != $wp_query->query_vars['name']) {
        global $wpdb;
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (!empty($wp_query->query_vars['pagename'])) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $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, $wp_query->query_vars['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 ('' != $wp_query->query_vars['year']) {
            $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
        }
        if ('' != $wp_query->query_vars['monthnum']) {
            $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
        }
        if ('' != $wp_query->query_vars['day']) {
            $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
        }
        $id = (int) $wpdb->get_var($query);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: 4.2

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 *
 * @uses $wp_query
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @return null If no link is found, null is returned.
 */
function wp_old_slug_redirect()
{
    global $wp_query;
    if (is_404() && '' != $wp_query->query_vars['name']) {
        global $wpdb;
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (!empty($wp_query->query_vars['pagename'])) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = reset($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $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, $wp_query->query_vars['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 ('' != $wp_query->query_vars['year']) {
            $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
        }
        if ('' != $wp_query->query_vars['monthnum']) {
            $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
        }
        if ('' != $wp_query->query_vars['day']) {
            $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
        }
        $id = (int) $wpdb->get_var($query);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: 4.1

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 *
 * @uses $wp_query
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @return null If no link is found, null is returned.
 */
function wp_old_slug_redirect()
{
    global $wp_query;
    if (is_404() && '' != $wp_query->query_vars['name']) {
        global $wpdb;
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (!empty($wp_query->query_vars['pagename'])) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = array_shift($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $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, $wp_query->query_vars['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 ('' != $wp_query->query_vars['year']) {
            $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
        }
        if ('' != $wp_query->query_vars['monthnum']) {
            $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
        }
        if ('' != $wp_query->query_vars['day']) {
            $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
        }
        $id = (int) $wpdb->get_var($query);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}

WordPress Version: 3.7

/**
 * Redirect old slugs to the correct permalink.
 *
 * Attempts to find the current slug from the past slugs.
 *
 * @since 2.1.0
 * @uses $wp_query
 * @uses $wpdb
 *
 * @return null If no link is found, null is returned.
 */
function wp_old_slug_redirect()
{
    global $wp_query;
    if (is_404() && '' != $wp_query->query_vars['name']) {
        global $wpdb;
        // Guess the current post_type based on the query vars.
        if (get_query_var('post_type')) {
            $post_type = get_query_var('post_type');
        } elseif (!empty($wp_query->query_vars['pagename'])) {
            $post_type = 'page';
        } else {
            $post_type = 'post';
        }
        if (is_array($post_type)) {
            if (count($post_type) > 1) {
                return;
            }
            $post_type = array_shift($post_type);
        }
        // Do not attempt redirect for hierarchical post types
        if (is_post_type_hierarchical($post_type)) {
            return;
        }
        $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, $wp_query->query_vars['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 ('' != $wp_query->query_vars['year']) {
            $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
        }
        if ('' != $wp_query->query_vars['monthnum']) {
            $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
        }
        if ('' != $wp_query->query_vars['day']) {
            $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
        }
        $id = (int) $wpdb->get_var($query);
        if (!$id) {
            return;
        }
        $link = get_permalink($id);
        if (!$link) {
            return;
        }
        wp_redirect($link, 301);
        // Permanent redirect
        exit;
    }
}