Skip to content

Instantly share code, notes, and snippets.

@scribu
Created July 27, 2011 22:49
Show Gist options
  • Save scribu/1110537 to your computer and use it in GitHub Desktop.
Save scribu/1110537 to your computer and use it in GitHub Desktop.
CPT Redirect
<?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