Want more features on Pastebin? Sign Up, it's FREE!
Guest

Slug

By: a guest on Jun 13th, 2011  |  syntax: PHP  |  size: 1.35 KB  |  views: 126  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. // change this this to strip old slugs if needed:
  3. //update wp_posts set post_name = '' where guid like '%.asp'
  4. set_time_limit(20000);
  5.  
  6. /** Loads the WordPress Environment and Template, allowing wp functions like the_title() */
  7. define('WP_USE_THEMES', false);
  8. require('./wp-blog-header.php');
  9.  
  10. function bleach($which)
  11. {
  12.     $result = sanitize_title(get_the_title($which));
  13.     return $result;
  14. }
  15.  
  16. $dbhost = 'localhost';
  17. $dbuser = 'username';
  18. $dbpass = 'pass';
  19. $dbname = 'db';
  20.  
  21. $sql = 'SELECT ID, post_title' . ' FROM `wp_posts`' . ' WHERE post_status = "publish"' . " and post_name = '' " . ' order by ID asc';
  22.  
  23. $db = mysql_connect($dbhost, $dbuser, $dbpass) or die('Could not connect: ' . mysql_error());
  24. mysql_select_db($dbname);
  25.  
  26. $result = mysql_query($sql) or die('<strong>Query failed: ' . mysql_error());
  27. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  28.     $id         = $row['ID'];
  29.     $title      = $row['post_title'];
  30.     $clean_slug = rawurlencode(bleach($id));
  31.  
  32.     echo "ID<em>:{$row['ID']} " . "post_title : {$title} " . "sanitized : {$clean_slug} <br>";
  33.     $sql_u = "UPDATE `wp_posts` SET post_name = '" . $clean_slug . "' " . 'WHERE ID = ' . $id;
  34.     echo '<strong>QUERY:</strong>' . $sql_u . '<br>';
  35.     mysql_query($sql_u) or die('ERROR: ' . mysql_error());
  36.     flush();
  37. }
  38. echo "</em></strong>";
  39. mysql_close($db);
  40. ?>
clone this paste RAW Paste Data