Useful WordPress SQL Hacks

by admin on August 20, 2010

I often find myself in my daily work coming up to some issue’s that I need to find some answer’s for.  While looking yesterday I came across a great article that helped me out a lot.  It helped me complete some SQL issues I was having while moving some WordPress databases to a new server.  This helped me and I hope you will find it useful as well.

1. Creating a Backup of Your Database

Sm5 in 8 Useful WordPress SQL Hacks

The problem. While the tips in the rest of this post have been tested, you should definitely not try any of them without first having a proper backup of your MySQL database.

The solution. To create a manual backup of your WordPress database, follow these simple steps:

  1. Log in to phpMyAdmin and select your WordPress database.
  2. Once done, click the “Export” button located in the horizontal menu.
  3. Choose a compression method (personally, I use gzip), and click the “Execute” button.
  4. Your browser will ask you if you want to download the backup. Of course, select “Yes,” and then store it on your hard drive.

Explanation. Note that creating a backup of your WordPress database can be more easily executed with the WP-DB-Backup plug-in. WordPress users should install this plug-in if they have not yet done so and create regular backups of their data.

2. Batch Delete Post Revisions

Sm1 in 8 Useful WordPress SQL Hacks

The problem. Post revisions, a new WordPress 2.6 feature, can be very useful, but they also increase the size of your MySQL database. Sure, you can manually delete posts revisions, but that’s very long and boring work.

The solution. The solution to this problem is simple: we batch delete post revisions by using a simple SQL query. The result can be almost unbelievable if you have a lot of posts: Your database size will be reduced by half!

  1. Log in to phpMyAdmin and select your WordPress database.
  2. Click the “SQL” button. Paste the following code in the SQL command window:
    1 DELETE FROM wp_posts WHERE post_type = "revision";
  3. You’re done. Depending on how many posts you had in your WordPress database, you may have saved lots of precious space!

Code explanation. The wp_posts table has a field named post_type. This field can have one of many values, such as “post,” “page” or “revision.” When we want to get rid of post revisions, we simply run a command to delete any entry in the wp_posts table in which the post_type field is equal to “revision.”

3. Erase 5000 Spam Comments in a Second

Sm3 in 8 Useful WordPress SQL Hacks

The problem. True story: a friend of mine recently created his own blog and started to promote it everywhere on the Internet. After some weeks of intensive work, he spent some days on vacation without Internet access.

When he came back home, he looked at his blog and saw… 5000+ comments awaiting moderation! Of course, most of them were spam, but he was actually about to check them all to make sure he did not delete a valid comment made by one of his regular readers.

The solution. Happily, my friend told me about his spam problem. He had already spent 45 minute manually deleting spam when I showed him this useful SQL tip.

  1. Log in to phpMyAdmin and select your WordPress database.
  2. Click the “SQL” button. Paste the following code in the SQL command window:
    1 DELETE from wp_comments WHERE comment_approved = '0';
  3. Goodbye bad comments! Enjoy your spam-free database!

Explanation. The wp_comments table contains a field named comment_approved, which is a boolean value (1 or 0). Approved comments have a value of 1, and comments awaiting moderation have a value of 0. By running the above command, we simply delete any comments that haven’t been approved yet.

Be careful. While this solution can be pretty useful if you have millions of spam comments to delete, it will also erase valid unapproved comments. If you don’t already use Akismet, install it now to prevent spamming.

4. Change the Post Attribution

Sm4 in 8 Useful WordPress SQL Hacks

The problem. When you installed WordPress, an “admin” account was created. Some bloggers make the mistake of using that account to write their posts, until they realize that it’s not personal at all.

The solution. Modifying author attribution on each post takes a lot of time. Happily, SQL can help you get things done:

  1. Log in to your phpMyAdmin and select your WordPress database.
  2. First, we have to get the right user IDs. To do so, open the SQL command window and execute the following command:
    1 SELECT ID, display_name FROM wp_users;
  3. phpMyAdmin will display a list of user IDs associated with WordPress users. Let’s say that NEW_AUTHOR_ID is the ID of the more recently created author, and OLD_AUTHOR_ID is the original admin account ID.
  4. After you swap the NEW_AUTHOR_ID and OLD_AUTHOR_ID IDs, run the following command:
    1 UPDATE wp_posts SET post_author=NEW_AUTHOR_ID WHERE post_author=OLD_AUTHOR_ID;
  5. That’s all. All posts previously attributed to admin are now attributed to whichever valid user you have selected.

5. Manually Reset Your Password

Sm6 in 8 Useful WordPress SQL Hacks

The problem. In order to protect their blogs, people often pick strong passwords, such as u7*KoF5i8_. Of course, this is a good thing, but I have heard many stories of forgotten admin passwords.

The solution. When you lose your password, WordPress can email you a link to reset it. But if you don’t have access to the email address recorded in the WordPress database anymore, or if you prefer just running a simple command instead, here is the hack.

  1. Log in to your phpMyAdmin, select your WordPress database and open the SQL window.
  2. Insert the following command (assuming your username is “admin”):
    1 UPDATE `wp_users` SET `user_pass` = MD5('PASSWORD') WHERE `wp_users`.`user_login` =`admin` LIMIT 1;
  3. You’re done. Your password has been successfully replaced by whatever you inserted in space above marked “PASSWORD.”

Explanation. User passwords are stored in the wp_users table. Of course, an MD5 hash is used to secure the password.

We have to set up an “UPDATE” SQL request and use the built-in MD5() MySQL function to convert our password to MD5 and then update it. The “WHERE” clause ensures that we’re updating only the admin’s password. The same request without the “WHERE” clause would result in all passwords being updated!

6. Change Your WordPress Domain Name

Sm7 in 8 Useful WordPress SQL Hacks

The problem. Although it is not recommended, you may want at some point to change your domain name while keeping your blog and its data. Because WordPress records your domain name in the database, you have to change the database in order to connect your new domain name to your WordPress blog.

The solution.

  1. You guessed it: the first thing to do is log in to your phpMyAdmin and select your WordPress database.
  2. Click the “SQL” button to open the SQL command window. In order to change your WordPress URL, execute this first command:
    1 UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldsite.com', 'http://www.newsite.com') WHERE option_name = 'home' OR option_name = 'siteurl';
  3. Then, we have to replace the relative URL (guid) of each post. The following command will do that job:
    1 UPDATE wp_posts SET guid = replace(guid, 'http://www.oldsite.com','http://www.newsite.com');
  4. We’re almost done. The last thing to do is a search and replace in the wp_posts table to make sure that no absolute URL is still here:
    1 UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldsite.com', 'http://www.newsite.com');
  5. You’re done. You should be able to log in to your WordPress dashboard using your new URL.

Explanation. To easily change our WordPress domain name, I took advantage of the super-useful MySQL function “replace,” which allows you to replace one term by another.

7. Display the Number of SQL Queries on Your blog

Sm8 in 8 Useful WordPress SQL Hacks

The problem. When trying to optimize your blog’s loading time, knowing the number of queries made to the database is important. In order to reduce queries, the first thing to know is how many queries are made on a single page.

The solution.

  1. This time, no need to log in to phpMyAdmin. Simply open the footer.php file in your theme and append the following lines of code:
    1 <?php if (is_user_logged_in()) { ?>
    2 <?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.
    3 <?php } ?>
  2. Save the file and visit your blog. In the footer, you’ll see the number of queries made to the WordPress database as well as the time it took to make them.

Explanation. Seems that many WordPress users aren’t aware of this useful function. The get_num_queries() function returns the number of executed queries during a page load.

Note that the above code will only display the number of queries to logged-in users, because regular visitors and search engine bots don’t need to know about it. But, if you’d like to make it public, simply remove the if (is_user_logged_in()) conditional instruction.

8. Restore Your WordPress Database

Sm9 in 8 Useful WordPress SQL Hacks

The problem. Let’s say, for some reason, such as a hacking or upgrade problem, you have lost your blog data or it has become corrupted. If you have a backup (and I hope you do!) you will have to import it to your WordPress database.

The solution.

  1. Log in to phpMyAdmin and select your WordPress database.
  2. Click the “Import” button in the horizontal menu.
  3. Click the “Browse” button and select the most recent database backup on your hard disk.
  4. Click the “Execute” button. If everything went well, your WordPress database is fully functional again.

This Article was found at Smashingmagazine.com

{ 0 comments }

Local Market Analyzed

by admin on September 11, 2009

As you will learn I am a local SEO here in Hickory, NC.  I have thought about many things I could do with this site, and I am learning towards targeting the Hickory Market and doing reviews of local sites and listing the Best sites, and my experiences around town.

With this being said if you would like to me review your site or would like to exchange links you can email me at info@birdseyeseo.com. With that being said, check back often for reviews and discussion about local companies.

{ 0 comments }

PHP Landing Page Script Trick

April 22, 2009

I would like to take a few minutes and show everyone a simple trick that will allow you to make keyword based landing pages.  I will also use this script to build pages on my sites which google will index and so technically they will be hosting pages for me. First thing you will do […]

Read the full article →

Using Twitter to Take Your Business to the Next Level

April 21, 2009

Using twitter to take your business to the next level Create a twitter account – Go to TweetLater or another client and set up a automated welcome DM. This will be a free impression to people who follow you. Now there is a trick to a DM, and this is what I have done.  I […]

Read the full article →

Article Marketing For Local Markets

April 17, 2009

At a recent conference I was in a discussion about article marketing for links.  The whole recap was basically you would get 10 articles to start out with.  You would begin submitting 5 articles a day to various article directories.  along the way you would progressively add more submissions every day.  Eventually you would have […]

Read the full article →