整理的一些實用WordPress后臺MySQL操作命令
Jun 13, 2016 am 11:55 AM
不過假設(shè)你的WordPress網(wǎng)站上有成百上千篇文章,而你需要進行全站范圍的改動, 這時從后臺逐條編輯就有點費時費力了,并且犯錯的幾率也會提高。 最好的方法是進入WordPress的MySQL數(shù)據(jù)庫執(zhí)行必要的查詢(改動)。 通過MySQL可以迅速地完成以上任務(wù),為你節(jié)省更多時間。
下面要介紹的就是一些省時省力的WordPress SQL查詢方法。
事先備份
WordPress數(shù)據(jù)庫里存儲了你精心發(fā)表的每一篇文章,來自你的讀者的所有評論,以及你對自己網(wǎng)站進行的所有個性化設(shè)置。 因此,無論你對自己有多自信,都請記住一定要事先備份WordPress數(shù)據(jù)庫。 你可以通過備份插件進行備份。
為所有文章和頁面添加自定義字段
這段代碼可以為WordPress數(shù)據(jù)庫內(nèi)所有文章和頁面添加一個自定義字段。 你需要做的就是把代碼中的‘UniversalCutomField‘替換成你需要的文字,然后把‘MyValue‘改成需要的值。
復(fù)制代碼 代碼如下:
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');
如果只需要為文章添加自定義字段,可以使用下面這段代碼:
復(fù)制代碼 代碼如下:
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';
如果只需要為頁面添加自定義字段,可以使用下面這段代碼:
復(fù)制代碼 代碼如下:
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';
刪除文章meta數(shù)據(jù)
當(dāng)你安裝或刪除插件時,系統(tǒng)通過文章meta標(biāo)簽存儲數(shù)據(jù)。 插件被刪除后,數(shù)據(jù)依然會存留在post_meta表中,當(dāng)然這時你已經(jīng)不再需要這些數(shù)據(jù),完全可以刪除之。 記住在運行查詢前把代碼里的‘YourMetaKey‘替換成你需要的相應(yīng)值。
復(fù)制代碼 代碼如下:
DELETE FROM wp_postmeta WHERE meta_key = 'YourMetaKey';
查找無用標(biāo)簽
如果你在WordPress數(shù)據(jù)庫里執(zhí)行查詢刪除舊文章,和之前刪除插件時的情況一樣,文章所屬標(biāo)簽會留在數(shù)據(jù)庫里,并且還會出現(xiàn)在標(biāo)簽列表/標(biāo)簽云里。 下面的查詢可以幫你找出無用的標(biāo)簽。
復(fù)制代碼 代碼如下:
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;
批量刪除垃圾評論
執(zhí)行以下SQL命令:
復(fù)制代碼 代碼如下:
DELETE FROM wp_comments WHERE wp_comments.comment_approved = 'spam';
批量刪除所有未審核評論
這個SQL查詢會刪除你的網(wǎng)站上所有未審核評論,不影響已審核評論。
復(fù)制代碼 代碼如下:
DELETE FROM wp_comments WHERE comment_approved = 0
禁止評論較早文章
指定comment_status的值為open、closed或registered_only。
此外還需要設(shè)置日期(修改代碼中的2010-01-01):
復(fù)制代碼 代碼如下:
UPDATE wp_posts SET comment_status = 'closed' WHERE post_date
停用/激活trackback與pingback
指定comment_status的值為open、closed或registered_only。
向所有用戶激活pingbacks/trackbacks:
復(fù)制代碼 代碼如下:
UPDATE wp_posts SET ping_status = 'open';
向所有用戶禁用pingbacks/trackbacks:
復(fù)制代碼 代碼如下:
UPDATE wp_posts SET ping_status = 'closed';
激活/停用某一日期前的Pingbacks & Trackbacks
指定ping_status的值為open、closed或registered_only。
此外還需要設(shè)置日期(修改代碼中的2010-01-01):
復(fù)制代碼 代碼如下:
UPDATE wp_posts SET ping_status = 'closed' WHERE post_date
刪除特定URL的評論
當(dāng)你發(fā)現(xiàn)很多垃圾評論都帶有相同的URL鏈接,可以利用下面的查詢一次性刪除這些評論。%表示含有“%"符號內(nèi)字符串的所有URL都將被刪除。
復(fù)制代碼 代碼如下:
DELETE from wp_comments WHERE comment_author_url LIKE "%nastyspamurl%" ;
識別并刪除“X"天前的文章
查找“X"天前的所有文章(注意把X替換成相應(yīng)數(shù)值):
復(fù)制代碼 代碼如下:
SELECT * FROM `wp_posts` WHERE `post_type` = 'post'AND DATEDIFF(NOW(), `post_date`) > X
刪除“X"天前的所有文章:
復(fù)制代碼 代碼如下:
DELETE FROM `wp_posts` WHERE `post_type` = 'post'AND DATEDIFF(NOW(), `post_date`) > X
刪除不需要的短代碼
當(dāng)你決定不再使用短代碼時,它們不會自動消失。你可以用一個簡單的SQL查詢命令刪除所有不需要的短代碼。 把“tweet"替換成相應(yīng)短代碼名稱:
復(fù)制代碼 代碼如下:
UPDATE wp_post SET post_content = replace(post_content, '[tweet]', '' ) ;
將文章轉(zhuǎn)為頁面
依然只要通過PHPMyAdmin運行一個SQL查詢就可以搞定:
復(fù)制代碼 代碼如下:
UPDATE wp_posts SET post_type = 'page' WHERE post_type = 'post'
將頁面轉(zhuǎn)換成文章:
復(fù)制代碼 代碼如下:
UPDATE wp_posts SET post_type = 'post' WHERE post_type = 'page'
更改所有文章上的作者屬性
首先通過下面的SQL命令檢索作者的ID:
復(fù)制代碼 代碼如下:
SELECT ID, display_name FROM wp_users;
成功獲取該作者的新舊ID后,插入以下命令,記住用新作者ID替換NEW_AUTHOR_ID,舊作者ID替換OLD_AUTHOR_ID。
復(fù)制代碼 代碼如下:
UPDATE wp_posts SET post_author=NEW_AUTHOR_ID WHERE post_author=OLD_AUTHOR_ID;
批量刪除文章修訂歷史
文章修訂歷史保存可以很實用,也可以很讓人煩惱。 你可以手動刪除修訂歷史,也可以利用SQL查詢給自己節(jié)省時間。
復(fù)制代碼 代碼如下:
DELETE FROM wp_posts WHERE post_type = "revision";
停用/激活所有WordPress插件
激活某個插件后發(fā)現(xiàn)無法登錄WordPress管理面板了,試試下面的查詢命令吧,它會立即禁用所有插件,讓你重新登錄。
復(fù)制代碼 代碼如下:
UPDATE wp_options SET option_value = 'a:0:{}' WHERE option_name = 'active_plugins';
更改WordPress網(wǎng)站的目標(biāo)URL
把WordPress博客(模板文件、上傳內(nèi)容&數(shù)據(jù)庫)從一臺服務(wù)器移到另一臺服務(wù)器后,接下來你需要告訴WordPress你的新博客地址。
使用以下命令時,注意將http://www.old-site.com換成你的原URL,http://blog.doucube.com換成新URL地址。
首先:
復(fù)制代碼 代碼如下:
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';
然后利用下面的命令更改wp_posts里的URL:
復(fù)制代碼 代碼如下:
UPDATE wp_posts SET guid = replace(guid, 'http://www.old-site.com','http://blog.doucube.com);
最后,搜索文章內(nèi)容以確保新URL鏈接與原鏈接沒有弄混:
復(fù)制代碼 代碼如下:
UPDATE wp_posts SET post_content = replace(post_content, ' http://www.ancien-site.com ', ' http://blog.doucube.com ');
更改默認(rèn)用戶名Admin
把其中的YourNewUsername替換成新用戶名。
復(fù)制代碼 代碼如下:
UPDATE wp_users SET user_login = 'YourNewUsername' WHERE user_login = 'Admin';
手動重置WordPress密碼
如果你是你的WordPress網(wǎng)站上的唯一作者,并且你沒有修改默認(rèn)用戶名, 這時你可以用下面的SQL查詢來重置密碼(把其中的PASSWORD換成新密碼):
復(fù)制代碼 代碼如下:
UPDATE `wordpress`.`wp_users` SET `user_pass` = MD5('PASSWORD')
WHERE `wp_users`.`user_login` =`admin` LIMIT 1;
搜索并替換文章內(nèi)容
OriginalText換成被替換內(nèi)容,ReplacedText換成目標(biāo)內(nèi)容:
復(fù)制代碼 代碼如下:
UPDATE wp_posts SET `post_content` = REPLACE (`post_content`, 'OriginalText','ReplacedText');
更改圖片URL
下面的SQL命令可以幫你修改圖片路徑:
復(fù)制代碼 代碼如下:
UPDATE wp_postsSET post_content = REPLACE (post_content, 'src="http://www.myoldurl.com', 'src="http://blog.doucube.com');

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

mysqldump is a common tool for performing logical backups of MySQL databases. It generates SQL files containing CREATE and INSERT statements to rebuild the database. 1. It does not back up the original file, but converts the database structure and content into portable SQL commands; 2. It is suitable for small databases or selective recovery, and is not suitable for fast recovery of TB-level data; 3. Common options include --single-transaction, --databases, --all-databases, --routines, etc.; 4. Use mysql command to import during recovery, and can turn off foreign key checks to improve speed; 5. It is recommended to test backup regularly, use compression, and automatic adjustment.

When handling NULL values ??in MySQL, please note: 1. When designing the table, the key fields are set to NOTNULL, and optional fields are allowed NULL; 2. ISNULL or ISNOTNULL must be used with = or !=; 3. IFNULL or COALESCE functions can be used to replace the display default values; 4. Be cautious when using NULL values ??directly when inserting or updating, and pay attention to the data source and ORM framework processing methods. NULL represents an unknown value and does not equal any value, including itself. Therefore, be careful when querying, counting, and connecting tables to avoid missing data or logical errors. Rational use of functions and constraints can effectively reduce interference caused by NULL.

GROUPBY is used to group data by field and perform aggregation operations, and HAVING is used to filter the results after grouping. For example, using GROUPBYcustomer_id can calculate the total consumption amount of each customer; using HAVING can filter out customers with a total consumption of more than 1,000. The non-aggregated fields after SELECT must appear in GROUPBY, and HAVING can be conditionally filtered using an alias or original expressions. Common techniques include counting the number of each group, grouping multiple fields, and filtering with multiple conditions.

MySQL supports transaction processing, and uses the InnoDB storage engine to ensure data consistency and integrity. 1. Transactions are a set of SQL operations, either all succeed or all fail to roll back; 2. ACID attributes include atomicity, consistency, isolation and persistence; 3. The statements that manually control transactions are STARTTRANSACTION, COMMIT and ROLLBACK; 4. The four isolation levels include read not committed, read submitted, repeatable read and serialization; 5. Use transactions correctly to avoid long-term operation, turn off automatic commits, and reasonably handle locks and exceptions. Through these mechanisms, MySQL can achieve high reliability and concurrent control.

To view the size of the MySQL database and table, you can query the information_schema directly or use the command line tool. 1. Check the entire database size: Execute the SQL statement SELECTtable_schemaAS'Database',SUM(data_length index_length)/1024/1024AS'Size(MB)'FROMinformation_schema.tablesGROUPBYtable_schema; you can get the total size of all databases, or add WHERE conditions to limit the specific database; 2. Check the single table size: use SELECTta

Character set and sorting rules issues are common when cross-platform migration or multi-person development, resulting in garbled code or inconsistent query. There are three core solutions: First, check and unify the character set of database, table, and fields to utf8mb4, view through SHOWCREATEDATABASE/TABLE, and modify it with ALTER statement; second, specify the utf8mb4 character set when the client connects, and set it in connection parameters or execute SETNAMES; third, select the sorting rules reasonably, and recommend using utf8mb4_unicode_ci to ensure the accuracy of comparison and sorting, and specify or modify it through ALTER when building the library and table.

To set up asynchronous master-slave replication for MySQL, follow these steps: 1. Prepare the master server, enable binary logs and set a unique server-id, create a replication user and record the current log location; 2. Use mysqldump to back up the master library data and import it to the slave server; 3. Configure the server-id and relay-log of the slave server, use the CHANGEMASTER command to connect to the master library and start the replication thread; 4. Check for common problems, such as network, permissions, data consistency and self-increase conflicts, and monitor replication delays. Follow the steps above to ensure that the configuration is completed correctly.

The most direct way to connect to MySQL database is to use the command line client. First enter the mysql-u username -p and enter the password correctly to enter the interactive interface; if you connect to the remote database, you need to add the -h parameter to specify the host address. Secondly, you can directly switch to a specific database or execute SQL files when logging in, such as mysql-u username-p database name or mysql-u username-p database name
