


Some practical WordPress backend MySQL operation commands compiled_PHP tutorial
However, suppose you have hundreds or thousands of articles on your WordPress website and you need to make site-wide changes. At this time, editing one by one from the background is a bit time-consuming and laborious, and the chance of making mistakes will increase. The best way is to go into WordPress’s MySQL database and perform the necessary queries (changes). The above tasks can be completed quickly through MySQL, saving you more time.
The following are some time-saving and labor-saving WordPress SQL query methods.
Back up in advance
The WordPress database stores every article you carefully publish, all comments from your readers, and all the changes you make to your website Personalization. Therefore, no matter how confident you are, please remember to back up your WordPress database beforehand. You can back up via the backup plugin.
Add a custom field to all posts and pages
This code can add a custom field to all posts and pages in the WordPress database. All you need to do is replace ‘UniversalCutomField’ in the code with the text you need, and then change ‘MyValue’ to the required value.
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 to the article, you can Use the following code:
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:
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 post meta data
When you install or remove the plug-in, the system passes the post meta Tags store data. After the plug-in is deleted, the data will still 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 corresponding value you need before running the query.
DELETE FROM wp_postmeta WHERE meta_key = 'YourMetaKey';
Find useless tags
If you execute a query in the WordPress database to delete old articles, just like when you deleted the plugin before, the tags to which the article belongs will remain in the database and will also appear in the tag list/tag cloud. The following query can help you find useless tags.
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;
Delete spam comments in batches
Execute the following SQL command:
DELETE FROM wp_comments WHERE wp_comments.comment_approved = 'spam';
Delete all unmoderated comments in batches
This SQL query will Delete all unmoderated comments on your site without affecting moderated comments.
DELETE FROM wp_comments WHERE comment_approved = 0
Disable comments Older article
Specify the value of comment_status as open, closed or registered_only.
In addition, you need to set the date (modify 2010-01-01 in the code):
UPDATE wp_posts SET comment_status = 'closed' WHERE post_date < '2010-01-01' AND post_status = 'publish';
Deactivate/activate trackback and pingback
Specify the value of comment_status as open, closed or registered_only.
Activate pingbacks/trackbacks for all users:
UPDATE wp_posts SET ping_status = 'open ';
Disable pingbacks/trackbacks for all users:
UPDATE wp_posts SET ping_status = 'closed';
Activate/deactivate Pingbacks & Trackbacks before a certain date
Specify the value of ping_status as open, closed or registered_only.
In addition, you need to set the date (modify 2010-01-01 in the code):
UPDATE wp_posts SET ping_status = 'closed' WHERE post_date < '2010-01-01' AND post_status = 'publish';
Delete comments for specific URL
When you find a lot of spam The comments all have the same URL link, and you can use the following query to delete these comments at once. % means that all URLs containing strings within the "%" symbol will be deleted.
DELETE from wp_comments WHERE comment_author_url LIKE "%nastyspamurl%" ;
Identify and delete articles from "X" days ago
Find all articles from "X" days ago (note to replace X with the corresponding value):
SELECT * FROM `wp_posts` WHERE `post_type` = 'post'AND DATEDIFF(NOW(), `post_date`) > X
Delete all posts from " ` WHERE `post_type` = 'post'AND DATEDIFF(NOW(), `post_date`) > , they will not disappear automatically. You can remove all unwanted shortcodes with a simple SQL query command. Replace "tweet" with the corresponding shortcode name:
UPDATE wp_post SET post_content = replace( post_content, '[tweet]', '' ) ;
Convert the article to a page
It can still be done by running a SQL query through PHPMyAdmin:
UPDATE wp_posts SET post_type = 'page' WHERE post_type = 'post'
Convert the page to Article:
First retrieve the author's ID via the following SQL command:
SELECT ID, display_name FROM wp_users;
After successfully obtaining the old and new ID of the author, insert the following command, remember to replace NEW_AUTHOR_ID with the new author ID and the old author ID replaces OLD_AUTHOR_ID.
Saving article revision history can be very practical or very annoying. You can manually delete the revision history, or you can use SQL queries to save yourself time.
DELETE FROM wp_posts WHERE post_type = "revision";
Deactivate/activate all WordPress plug-ins
After activating a plug-in, you find that you cannot log in to the WordPress management panel. Try the query command below. It will immediately disable all plug-ins and allow you to log in again.
UPDATE wp_options SET option_value = 'a:0:{}' WHERE option_name = 'active_plugins';
Change the target URL of the WordPress site
Put the WordPress blog (template file , upload content & database) after moving from one server to another, next you need to tell WordPress your new blog address.
When using the following commands, be sure to replace http://www.old-site.com with your original URL and http://blog.doucube.com with the new URL address.
First:
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:
UPDATE wp_posts SET guid = replace( guid, 'http://www.old-site.com','http://blog.doucube.com);
Finally, search the article content to ensure that the new URL link is the same as the original link No confusion:
UPDATE wp_posts SET post_content = replace(post_content, ' http:// www.ancien-site.com ', ' http://blog.doucube.com ');
Change the default username Admin
Replace YourNewUsername with the new username.
UPDATE wp_users SET user_login = 'YourNewUsername' WHERE user_login = 'Admin';
Manually reset WordPress password
If you are the only author on your WordPress website and you have not modified the default username, then you can use the following SQL query to reset the password (put where Change the PASSWORD to a new password):
UPDATE `wordpress`.`wp_users` SET ` user_pass` = MD5('PASSWORD')
WHERE `wp_users`.`user_login` =`admin` LIMIT 1;
Search and replace article content
OriginalText is replaced with the replaced content , ReplacedText is replaced with the target content:
UPDATE wp_posts SET `post_content` = REPLACE (`post_content `, 'OriginalText','ReplacedText');
Change the image URL
The following SQL command can help you change the image path:
UPDATE wp_postsSET post_content = REPLACE (post_content, 'src="http://www.myoldurl.com', 'src="http:// blog.doucube.com');

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

You can view the WordPress front-end by logging into the dashboard and switching to the View Sites tab; automate the viewing process with a headless browser; installing the WordPress plugin to preview the front-end within the dashboard; viewing the front-end via a local URL (if WordPress is set locally).
