


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

AI Hentai Generator
Generate AI Hentai for free.

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

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
