Skip to content

Instantly share code, notes, and snippets.

@bwonur
Created July 18, 2019 09:48
Show Gist options
  • Save bwonur/866caca6434b0790538be8f86a57b46e to your computer and use it in GitHub Desktop.
Save bwonur/866caca6434b0790538be8f86a57b46e to your computer and use it in GitHub Desktop.
wp-unique-post-slug-numeric.php
<?php
function wp_unique_post_slug($col,$table='wp_posts'){
global $wpdb;
$alphabet = array_merge( range(0, 9), range('a','z') );
$already_exists = true;
do {
$guidchr = array();
for ($i=0; $i<32; $i++)
$guidchr[] = $alphabet[array_rand( $alphabet )];
$guid = sprintf( "%s", implode("", array_slice($guidchr, 0, 12, true)) );
// check that GUID is unique
$already_exists = (boolean) $wpdb->get_var("
SELECT COUNT($col) as the_amount FROM $table WHERE $col = '$guid'
");
} while (true == $already_exists);
return $guid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment