Created
February 26, 2016 21:17
-
-
Save kingkool68/e2f4c1860912501020b6 to your computer and use it in GitHub Desktop.
Use the GUID field to maintain old URL redirects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function rh_404_overrides() { | |
global $wp_query, $wpdb; | |
if( $wp_query->is_404 && isset( $wp_query->query_vars['pagename'] ) && !empty( $wp_query->query_vars['pagename'] ) ) { | |
$query = "SELECT `ID` FROM `" . $wpdb->posts . "` WHERE `guid` LIKE '%" . $wp_query->query_vars['pagename'] . "%' LIMIT 0,1;"; | |
$post_id = intval( $wpdb->get_var( $query ) ); | |
if( $url = get_permalink( $post_id ) ) { | |
wp_redirect( $url, 301 ); | |
die(); | |
} | |
} | |
} | |
add_filter( 'template_redirect', 'rh_404_overrides' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment