Title: Update post/slug
Last modified: January 31, 2024

---

# Update post/slug

 *  Resolved [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * (@ayrancd)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/update-post-slug/)
 * When you create a post using **wp_insert_post **you don’t have to set the slug/
   permalink since it gets automatically based on the title, what about when you
   update the same post using **wp_update_post**? How do we update the slug again
   based on the post title?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/update-post-slug/#post-17388708)
 * If you set this as an attribute, WordPress will create a new slug for the post
   based on the current title:
 *     ```wp-block-code
       'post_name' => ''
       ```
   
 * See: [https://wordpress.stackexchange.com/questions/105926/rewriting-post-slug-before-post-save](https://wordpress.stackexchange.com/questions/105926/rewriting-post-slug-before-post-save)
 *  Thread Starter [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * (@ayrancd)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/update-post-slug/#post-17425324)
 * [@threadi](https://wordpress.org/support/users/threadi/) just tried the following
   code and the slug is not being updated at all
 *     ```wp-block-code
       wp_update_post(array('ID' => $id, 'post_name' => sanitize_title($_POST["product_title"])));
       ```
   
 * I also tried it empty and no changes.
    -  This reply was modified 2 years, 4 months ago by [Cezar Ayran](https://wordpress.org/support/users/ayrancd/).
 *  Thread Starter [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * (@ayrancd)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/update-post-slug/#post-17425722)
 * Just found a solution:
 *     ```wp-block-code
       function slug_save_post_callback( $post_ID, $post, $update ) {
           if ($post->post_type != 'product')
               return;
   
           $new_slug = sanitize_title( $post->post_title, $post_ID );
           if ($new_slug == $post->post_name)
               return; // already set
   
           // unhook this function to prevent infinite looping
           remove_action( 'save_post', 'slug_save_post_callback', 10, 3 );
           // update the post slug (WP handles unique post slug)
           wp_update_post( array(
               'ID' => $post_ID,
               'post_name' => $new_slug
           ));
           // re-hook this function
           add_action( 'save_post', 'slug_save_post_callback', 10, 3 );
       }
       add_action( 'save_post', 'slug_save_post_callback', 10, 3 );
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Update post/slug’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * Last activity: [2 years, 4 months ago](https://wordpress.org/support/topic/update-post-slug/#post-17425722)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
