The following PHP script updates missing post slugs (permalinks) in a WordPress blogs, or reformats them according to your needs:
"; <?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 = 'test'; $dbpass = 'test'; $dbname = 'WordPress'; $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 = bleach($id); echo "ID<em>:{$row['ID']} " . "post_title : {$title} " . "sanitized : {$clean_slug} "; $sql_u = "UPDATE `wp_posts` SET post_name = '" . $clean_slug . "' " . 'WHERE ID = ' . $id; echo '<strong>QUERY:</strong>' . $sql_u . ''; mysql_query($sql_u) or die('ERROR: ' . mysql_error()); flush(); } mysql_close($db); ?> </em></strong> |
Thank you great post, I use it to sanitize my slug’s on my blog. Great work!!!
thanks !
you just save my life 🙂