Last active
February 1, 2016 17:49
-
-
Save wpscholar/5e42d5479c9e0a814df4 to your computer and use it in GitHub Desktop.
Redirect all root-level URL paths for posts to the new permalink paths.
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_action( 'template_redirect', function () { | |
/** | |
* @var WP $wp | |
*/ | |
global $wp; | |
/** | |
* @var WP_Query $wp_query | |
*/ | |
global $wp_query; | |
if ( $wp_query->is_404() ) { | |
$query = new WP_Query( array( | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'name' => $wp->request, | |
'posts_per_page' => 1, | |
'nopaging' => true, | |
'no_found_rows' => true, | |
) ); | |
if ( $query->found_posts ) { | |
$redirect_url = add_query_arg( $wp->query_string, '', get_the_permalink( $query->post ) ); | |
wp_safe_redirect( $redirect_url, 301 ); | |
} | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment