Skip to content

Instantly share code, notes, and snippets.

@dejanbatanjac
Created August 10, 2018 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dejanbatanjac/14c1975da39304f2c1674026750be39b to your computer and use it in GitHub Desktop.
Save dejanbatanjac/14c1975da39304f2c1674026750be39b to your computer and use it in GitHub Desktop.
rewrite post slugs
<?php
// change this this to strip old slugs if needed:
//update wp_posts set post_name = '' where guid like '%.asp'
set_time_limit(20000);
/** Loads the WordPress Environment and Template, allowing wp functions like the_title() */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
function bleach($which)
{
$result = sanitize_title(get_the_title($which));
return $result;
}
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'storm23';
$dbname = 'wordpress_storm';
$sql = 'SELECT ID, post_title' . ' FROM `wp_posts`' . ' WHERE post_status = "publish"' . " and post_name = '' " . ' order by ID asc';
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die('Could not connect: ' . mysql_error());
mysql_select_db($dbname);
$result = mysql_query($sql) or die('<strong>Query failed: ' . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['ID'];
$title = $row['post_title'];
$clean_slug = rawurlencode(bleach($id));
echo "ID<em>:{$row['ID']} " . "post_title : {$title} " . "sanitized : {$clean_slug} <br>";
$sql_u = "UPDATE `wp_posts` SET post_name = '" . $clean_slug . "' " . 'WHERE ID = ' . $id;
echo '<strong>QUERY:</strong>' . $sql_u . '<br>';
mysql_query($sql_u) or die('ERROR: ' . mysql_error());
flush();
}
echo "</em></strong>";
mysql_close($db);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment