Last active
May 11, 2016 16:03
-
-
Save stephanieleary/06bd439a6a5b834bf02cec35208d0b39 to your computer and use it in GitHub Desktop.
Redirect 404 errors to posts with old path stored by HTML Import
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 | |
add_filter( 'redirect_canonical', 'my_404_no_guessing', 10, 2 ); | |
function my_404_no_guessing( $redirect_url, $request_url ) { | |
if ( is_404() ) { | |
// HTML Import redirects | |
$posts = get_posts( array( | |
'meta_key' => 'URL_before_HTML_Import', | |
'meta_value' => $request_url, | |
'post_type' => 'any', | |
'posts_per_page' => 1, | |
'fields' => 'ids', | |
) ); | |
if ( $posts ) { | |
$redirect_url = get_permalink( array_pop( $posts ) ); | |
} | |
else | |
return false; | |
} | |
return $redirect_url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment