Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this questionNeed to redirect to new structure /%year%/%monthnum%/%day%/%category%/%postname%
Saw code that helped before but can't seem to find it. Updated theme and lost the code that went into functions.php. Backup best practices are important!
UPDATE: Found the code again! It was first posted @ Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
add_filter( '404_template', 't5_redirect_to_category' );
function t5_redirect_to_category( $template )
{
if ( ! is_404() )
return $template;
global $wp_rewrite, $wp_query;
// change 'yourpermalink' to your new permalink structure
// my new structure is '/%year%/%monthnum%/%day%/%category%/%postname%'
if ( 'yourpermalink' !== $wp_rewrite->permalink_structure )
return $template;
if ( ! $post = get_page_by_path( $wp_query->query['category_name'], OBJECT, 'post' ) )
return $template;
$permalink = get_permalink( $post->ID );
wp_redirect( $permalink, 301 );
exit;
}
Stick this code in functions.php using the theme editor. The code can redirect all new permalink configurations that are currently returning as a 404 due to changes.
Remember to save this code because every time you update the theme it will be removed and you will need to add it to functions.php again.
This literally saved me. Thanks !
Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this questionNeed to redirect to new structure /%year%/%monthnum%/%day%/%category%/%postname%
Saw code that helped before but can't seem to find it. Updated theme and lost the code that went into functions.php. Backup best practices are important!
UPDATE: Found the code again! It was first posted @ Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
add_filter( '404_template', 't5_redirect_to_category' );
function t5_redirect_to_category( $template )
{
if ( ! is_404() )
return $template;
global $wp_rewrite, $wp_query;
// change 'yourpermalink' to your new permalink structure
// my new structure is '/%year%/%monthnum%/%day%/%category%/%postname%'
if ( 'yourpermalink' !== $wp_rewrite->permalink_structure )
return $template;
if ( ! $post = get_page_by_path( $wp_query->query['category_name'], OBJECT, 'post' ) )
return $template;
$permalink = get_permalink( $post->ID );
wp_redirect( $permalink, 301 );
exit;
}
Stick this code in functions.php using the theme editor. The code can redirect all new permalink configurations that are currently returning as a 404 due to changes.
Remember to save this code because every time you update the theme it will be removed and you will need to add it to functions.php again.
This literally saved me. Thanks https://wordpress.stackexchange/users/73/fuxia!
Share Improve this question edited Aug 4, 2019 at 17:32 Postard asked Aug 3, 2019 at 19:30 PostardPostard 113 bronze badges1 Answer
Reset to default 0I can only tell you how I handle things like this.
I swear by redirections which is a plugin that handles this sort of thing. It can be set to detect permalink changes and redirect old to new. Then you just go into perma-link and restructure as you wish. As someone who tends to figure out better ways to do things as he works, this is so much help for me.