Change permalink structure only for future posts
-
Hi, I am trying to change the permalink structure of blog posts only for the future posts without changing the old URLs due to their SEO scores.
When i try to change the permalink as https://www.domain.com/blog/catname/postname it works:
// rewriting post url function format_post_url( $post_link, $post, $leavename ) { if ( 'post' == get_post_type($post) && 'publish' == $post->post_status && strtotime('2017-04-01 00:00:00') < strtotime($post->post_date) ) { $post_link = rtrim($post_link,"/"); $post_pos = strrpos($post_link,"/"); $post = substr($post_link, $post_pos+1); // post name $post_link = substr($post_link, 0,strlen($post_link)-strlen($post)-1); $cat_pos = strpos($post_link,"/",8); $cat = substr($post_link, $cat_pos+1); // category name if(substr($cat,0,3)=="en/"){ $cat = substr($cat, 3); } $post_link = substr($post_link, 0,strlen($post_link)-strlen($cat)-1); return $post_link."/blog/".$cat."/".$post."/"; } return $post_link; } add_filter( 'post_link', 'format_post_url', 100, 3 );
But when i try to change the permalink as https://www.domain.com/postname it gives a 404 error, like following:
return $post_link."/".$post."/";
Please note that i can achieve this easily by changing the permalink structure via the WP backend but it will alter all the URLs, past & future, but i just want to change the future ones.
Can you guys please help me ? I even update the permalink structure settings in the wp backend after making these changes but it still doesn’t work.
- The topic ‘Change permalink structure only for future posts’ is closed to new replies.