mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
Remember old post slugs and automatically redirect to the new slug. fixes #3202
git-svn-id: https://develop.svn.wordpress.org/trunk@4556 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1116,6 +1116,40 @@ class WP_Query {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Redirect old slugs
|
||||
function wp_old_slug_redirect () {
|
||||
global $wp_query;
|
||||
if ( is_404() && '' != $wp_query->query_vars['name'] ) :
|
||||
global $wpdb;
|
||||
|
||||
$query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND meta_key = '_wp_old_slug' AND meta_value='" . $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 .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
|
||||
if ( '' != $wp_query->query_vars['monthnum'] )
|
||||
$query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
|
||||
if ( '' != $wp_query->query_vars['day'] )
|
||||
$query .= " AND DAYOFMONTH(post_date) = '{$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;
|
||||
endif;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Private helper functions
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user