Skip to content

Instantly share code, notes, and snippets.

@bwonur
Last active November 26, 2019 08:30
Show Gist options
  • Select an option

  • Save bwonur/cfff9f8223fb43e6d2565712a58b5b63 to your computer and use it in GitHub Desktop.

Select an option

Save bwonur/cfff9f8223fb43e6d2565712a58b5b63 to your computer and use it in GitHub Desktop.
WP Update Slug by ID. www.pealm.com
<?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