Home Backend Development PHP Tutorial Some practical WordPress backend MySQL operation commands compiled_PHP tutorial

Some practical WordPress backend MySQL operation commands compiled_PHP tutorial

Jul 21, 2016 pm 03:13 PM
mysql wordpress Backstage Order practical operate tidy of website

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.

Copy 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_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:
Copy 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:
Copy the 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 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.
Copy code The code is as follows:

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.
Copy code 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;

Delete spam comments in batches
Execute the following SQL command:
Copy code The code is as follows:

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.
Copy code The code is as follows:

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):
Copy the code The code is as follows:

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:
Copy code The code is as follows:

UPDATE wp_posts SET ping_status = 'open ';

Disable pingbacks/trackbacks for all users:
Copy code The code is as follows:

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):
Copy the code The code is as follows:

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.
Copy code The code is as follows:

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):
Copy code The code is as follows:

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:
Copy code The code is as follows:

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:
Copy code The code is as follows:

UPDATE wp_posts SET post_type = 'page' WHERE post_type = 'post'

Convert the page to Article:

Copy code The code is as follows:
UPDATE wp_posts SET post_type = 'post' WHERE post_type = 'page'

Change the author attribute on all articles
First retrieve the author's ID via the following SQL command:
Copy code The code is as follows :

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.

Copy code The code is as follows:
UPDATE wp_posts SET post_author=NEW_AUTHOR_ID WHERE post_author=OLD_AUTHOR_ID;

Delete article revision history in batches
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.
Copy code The code is as follows:

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.
Copy code The code is as follows:

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:
Copy code 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:
Copy the code The code is as follows:

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:
Copy the code 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 username Admin
Replace YourNewUsername with the new username.
Copy code The code is as follows:

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):
Copy code 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 article content
OriginalText is replaced with the replaced content , ReplacedText is replaced with the target content:
Copy code The code is as follows:

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:
Copy Code The code is as follows:

UPDATE wp_postsSET post_content = REPLACE (post_content, 'src="http://www.myoldurl.com', 'src="http:// blog.doucube.com');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326378.htmlTechArticleBut let’s say 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 is also...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

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: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

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.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

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.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

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.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

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 vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

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.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

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.

How to view the front-end of WordPress How to view the front-end of WordPress Apr 20, 2025 am 10:30 AM

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).

See all articles