Last active
November 26, 2019 08:30
-
-
Save bwonur/cfff9f8223fb43e6d2565712a58b5b63 to your computer and use it in GitHub Desktop.
WP Update Slug by ID. www.pealm.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function update_slug_by_id($slug, $id) { | |
| global $wpdb; | |
| // Update slug and make it unique | |
| $slug = (empty($slug)) ? get_the_title($id) : $slug; | |
| $slug = sanitize_title($slug); | |
| $new_slug = wp_unique_post_slug($slug, $id, get_post_status($id), get_post_type($id), null); | |
| $wpdb->query("UPDATE $wpdb->posts SET post_name = '$new_slug' WHERE ID = '$id'"); | |
| return $new_slug; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment