Hello I have a problem where i have 404 on posts after changing url with php using this function

wp_update_post( array(
                    'ID' => $post_id,
                    'post_name' => $new_slug

This happens in cases like the example below

if original post link abc.com/how-to-example
if I change to abc.com/how-to-example-passwd <-- redirect will still work
and I change again to a new permalink for third time redirect will work only for original link and new link and changes made between those two will return 404

Is it possible to keep all changes and make them redirect to current post using php?
I noticed wp automatically keep a custom field for the original link only

  • Why are you doing this? This appears to be a terrible idea.
    – vancoder
    Oct 18, 2021 at 15:39

1 Answer 1


You will need to add the following code in your htaccess.

Redirect 301 <Old Url> <New Url>

Place old link of your post in place of <Old Url> and new link in place of <New Url>

For reference view this link.

  • but i have tens of thousands of posts and permalinks might change frequently sometimes i might change 200 in a day also im using nginx Oct 18, 2021 at 7:49
  • You can try {wordpress.org/plugins/redirection} plugin. It will help you to manage the redirections.
    – BlueSuiter
    Oct 18, 2021 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.