Created
July 27, 2011 22:49
-
-
Save scribu/1110537 to your computer and use it in GitHub Desktop.
CPT Redirect
This file contains 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 | |
define( 'MY_CPT', 'actor' ); | |
/** | |
* Redirects /cary-grant/ to /actor/cary-grant/ or whatever the correct URL is | |
*/ | |
function redirect_old_cpt_urls() { | |
if ( !is_404() ) | |
return; | |
global $wp_query, $wpdb; | |
if ( empty( $wp_query->query['pagename'] ) || false !== strpos( $wp_query->query['pagename'], '/' ) ) | |
return; | |
$args = array( | |
'fields' => 'ids', | |
'post_type' => MY_CPT, | |
'name' => $wp_query->query['pagename'] | |
); | |
$posts = get_posts( $args ); | |
if ( count( $posts ) == 1 ) { | |
wp_redirect( get_permalink( $posts[0] ) . $get_params, 301 ); | |
exit(); | |
} | |
} | |
add_action( 'template_redirect', 'redirect_old_cpt_urls', 9 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment