Some practical WordPress MySQL Operation Commands

Source: Internet
Author: User
Tags wordpress database wordpress blog
WordPress stores all its information fragments (including articles, pages, comments, blog links, and plug-in Settings) in the MySQL database. Although WordPress users can control the above information snippets through website background editing, WordPress

However, if you have hundreds of articles on your WordPress website, and you need to make full-site changes, it will be time-consuming and laborious to edit one by one from the background, and the chance of making mistakes will also increase. The best way is to enter the MySQL database of WordPress to execute necessary queries (changes ). With MySQL, you can quickly complete the preceding tasks, saving you more time.

The following describes some time-saving and labor-saving WordPress SQL query methods.

Backup beforehand
The WordPress database stores every article you carefully published, all comments from your readers, and all your personalized settings for your website. Therefore, no matter how confident you are, remember to back up the WordPress database in advance. You can back up data through the backup plug-in.

Add custom fields for all articles and pages
This code adds a custom field to all articles and pages in the WordPress database. What you need to do is to replace 'universalcutomfield' in the code with the text you need, and then change 'myvalue' to the desired value.
The code is as follows:
Insert into wp_postmeta (post_id, meta_key, meta_value)
Select id as post_id, 'universalcustomfield'
AS meta_key 'myvalue AS meta_value FROM wp_postsWHERE id not in (SELECT post_id FROM wp_postmeta WHERE meta_key = 'universalcustomfield ');

If you only need to add a custom field for the article, you can use the following code:
The code is as follows:
Insert into wp_postmeta (post_id, meta_key, meta_value)
Select id as post_id, 'universalcustomfield'
AS meta_key 'myvalue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'universalcustomfield ') ''AND post_type = 'post ';

If you only need to add custom fields to the page, you can use the following code:
The code is as follows:
Insert into wp_postmeta (post_id, meta_key, meta_value)
Select id as post_id, 'universalcustomfield'
AS meta_key 'myvalue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'universalcustomfield ') AND 'Post _ type' = 'page ';

Delete article meta data
When you install or delete a plug-in, the system uses the meta tag to store data. After the plug-in is deleted, the data will remain in the post_meta table. of course, you no longer need the data and can delete it. Remember to replace 'yourmetakey 'in the code with the desired value before running the query.
The code is as follows:
Delete from wp_postmeta WHERE meta_key = 'yourmetakey ';

Search for useless tags
If you perform a query in the WordPress database to delete the old article, the tag of the article will remain in the database and appear in the tag list/tag cloud as before when you delete the plug-in. The following query can help you find useless tags.
The code is as follows:
SELECT * From wp_terms wtINNER JOIN wp_term_taxonomy wtt ON wt. term_id = wtt. term_id WHERE wtt. taxonomy = 'post _ tag' AND wtt. count = 0;

Batch delete spam comments
Run the following SQL command:
The code is as follows:
Delete from wp_comments WHERE wp_comments.comment_approved = 'spam ';

Batch delete all unreviewed comments
This SQL query will delete all unreviewed comments on your website and will not affect the reviewed comments.
The code is as follows:
Delete from wp_comments WHERE comment_approved = 0

Forbid comment on earlier articles
The value of comment_status is open, closed, or registered_only.
You also need to set the date (in the code ):
The code is as follows:
UPDATE wp_posts SET comment_status = 'closed 'WHERE post_date <'2017-01-01' AND post_status = 'Publish ';

Disable/activate trackback and pingback
The value of comment_status is open, closed, or registered_only.
Activate pingbacks/trackbacks to all users:
The code is as follows:
UPDATE wp_posts SET ping_status = 'open ';

Disable pingbacks/trackbacks for all users:
The code is as follows:
UPDATE wp_posts SET ping_status = 'closed ';

Activate/disable Pingbacks & Trackbacks before a certain date
The value of ping_status is open, closed, or registered_only.
You also need to set the date (in the code ):
The code is as follows:
UPDATE wp_posts SET ping_status = 'closed 'WHERE post_date <'2017-01-01' AND post_status = 'Publish ';

Delete a comment from a specific URL
When you find that many spam comments have the same URL link, you can use the following query to delete these comments at one time. % Indicates that all URLs containing strings in the "%" symbol will be deleted.
The code is as follows:
DELETE from wp_comments WHERE comment_author_url LIKE "% nastyspamurl % ";

Identify and delete the article "X" days ago
Search for all the articles "X" days ago (replace X with the corresponding value ):
The code is as follows:
SELECT * FROM 'WP _ posts' WHERE 'post _ type' = 'post' and datediff (NOW (), 'post _ date')> X

Delete all articles from "X" days ago:
The code is as follows:
Delete from 'WP _ posts' WHERE 'post _ type' = 'post' and datediff (NOW (), 'post _ date')> X

Delete unnecessary short code
When you decide not to use short code, they will not disappear automatically. You can use a simple SQL query command to delete all unneeded short code. Replace "tweet" with the corresponding short code name:
The code is as follows:
UPDATE wp_post SET post_content = replace (post_content, '[tweet]', '');

Convert an article to a page
You can still run an SQL query using PHPMyAdmin:
The code is as follows:
UPDATE wp_posts SET post_type = 'page' WHERE post_type = 'post'

Convert a page to an article:
The code is as follows:
UPDATE wp_posts SET post_type = 'post' WHERE post_type = 'page'

Change Author attributes of all articles
First, use the following SQL command to retrieve the author's ID:
The code is as follows:
Select id, display_name FROM wp_users;

After obtaining the new and old IDs of the Author, insert the following command, remember to replace NEW_AUTHOR_ID with the new author ID, and replace OLD_AUTHOR_ID with the old Author ID.
The code is as follows:
UPDATE wp_posts SET post_author = NEW_AUTHOR_ID WHERE post_author = OLD_AUTHOR_ID;

Batch delete Document Revision history
Saving revision history can be very practical and annoying. You can manually delete the revision history or use SQL queries to save time.
The code is as follows:
Delete from wp_posts WHERE post_type = "revision ";

Disable/activate all WordPress plug-ins
After activating a plug-in, you cannot log on to the WordPress management panel. try the following query command. it immediately disables all plug-ins and allows you to log on again.
The code is as follows:
UPDATE wp_options SET option_value = 'A: 0 :{} 'WHERE option_name = 'active _ ins ';

Change the target URL of a WordPress website
After you move a WordPress blog (Template File, uploaded content & database) from one server to another, you need to tell WordPress your new blog address.
When using the following command, note that you replace the http://www.old-site.com with your original URL, and the http://blog.doucube.com with a new URL address.
First:
The code is as follows:
UPDATE wp_options
SET option_value = replace (option_value, 'http: // www.old-site.com ', 'http: // blog.doucube.com ')
WHERE option_name = 'home' OR option_name = 'siteurl ';

Then, use the following command to change the URL in wp_posts:
The code is as follows:
UPDATE wp_posts SET guid = replace (guid, 'http: // www.old-site.com ', 'http: // blog.doucube.com );

Finally, search for the article content to ensure that the new URL link is not mixed with the original link:
The code is as follows:
UPDATE wp_posts SET post_content = replace (post_content, 'http://www.ancien-site.com ', 'http://blog.doucube.com ');

Change the default user name Admin
Replace YourNewUsername with the new user name.
The code is as follows:
UPDATE wp_users SET user_login = 'yourusername' WHERE user_login = 'admin ';

Manually reset the WordPress password
If you are the only author on your WordPress website and you have not modified the default username, you can reset the PASSWORD using the following SQL query (replace the PASSWORD with the new PASSWORD ):
The code is as follows:
UPDATE 'wordpress '. 'WP _ users' SET 'user _ pass' = MD5 ('password ')
WHERE 'WP _ users'. 'user _ login' = 'admin' LIMIT 1;

Search and replace the document content
Replace OriginalText with Replaced content, and ReplacedText with the target content:
The code is as follows:
UPDATE wp_posts SET 'post _ content' = REPLACE ('post _ content', 'ininaltext', 'replace text ');

Change Image URL
The following SQL command can help you modify the image path:
The code is as follows:
UPDATE wp_postsSET post_content = REPLACE (post_content, 'src = "http://www.myoldurl.com ', 'src =" http://blog.doucube.com ');
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.