Guest User

Untitled

a guest
Mar 1st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.77 KB | None | 0 0
  1. <?php
  2. $username="MY_MYSQL_USERNAME";
  3. $password="MY_MYSQL_PASSWORD";
  4. $hostname="MY_DATABASE_HOSTNAME"; //Your Database hostname usually
  5. $database="MY_DATABASE_NAME";
  6. $my_text = "test_post_meta";  // What I'm searching for
  7. $my_category = '8';    // The category to change it to
  8.  
  9. // Connect to MySQL and the database and verify:
  10. mysql_connect($hostname,$username,$password) or die(mysql_error());
  11.  
  12. echo "<p>Connected to MySQL.";
  13. mysql_select_db($database) or die(mysql_error());
  14. echo "<br />Connected to " . $database . "</p>";
  15.  
  16. // Verify what we're looking for, for troubleshooting:
  17. echo "<p><b>Looking for " . $my_text . "</b></p>";
  18.  
  19. // Get the ID field (which is WordPress's post
  20. // number) from any posts that have your text:
  21. $query = "SELECT ID FROM wp_posts WHERE post_content LIKE '%$my_text%'";
  22.  
  23. // Take those results and go through them:
  24. $result = mysql_query($query) or die(mysql_error());
  25.  
  26. // While there are results...
  27. while($row = mysql_fetch_array($result))
  28. {
  29. // Verify what we're doing -- changing post
  30. // number such-and-such...
  31. $thisPostHasIt = $row['ID'];
  32. echo "<p>Row " . $row['ID'] . " contains it, so...<br />";
  33.  
  34. // In the wp_term_relationships table,
  35. // update the category number ("term_taxonomy_id")
  36. // with the category number you specified -- but only
  37. // in one of the "result" rows.
  38. // We look for "object_id" to equal one of those
  39. // rows. (The object_id field refers to the WordPress
  40. // post number, just as the ID field did. Why two
  41. // different names? Who knows?)
  42.  
  43. mysql_query("UPDATE wp_term_relationships SET term_taxonomy_id='$my_category' WHERE object_id = '$thisPostHasIt'");
  44.  
  45. // And tell us about it:
  46. echo "Changing post number " . $thisPostHasIt . " to category number ". $my_category . "</p>";
  47. }
  48. echo "<p><b>All done!</b></p>";
  49. ?>
Add Comment
Please, Sign In to add comment