WordPress 常用數據庫SQL查詢語句大全

在使用WordPress的過程中,我們少不瞭要對數據庫進行修改操作,比如,更換域名、修改附件目錄、批量修改文章內容等等。這個時候,使用SQL查詢語句可以大大簡化我們的工作量。

關於如何操作SQL查詢語句,請移步《phpMyAdmin教程 之 使用SQL查詢語句修改數據庫信息》

下面分享一些wordpress常用的SQL查詢語句

註:1.在每次使用SQL查詢語句前,請務必導出數據庫備份!!

2.下面的SQL查詢語句,數據庫都是使用WordPress默認的 wp_ 表頭,請根據自己的實際修改。

1. 刪除所有未使用的標簽

1
2
3
4
5
DELETE a,b,c
FROM wp_terms AS a
LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN wp_term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id
WHERE c.taxonomy = 'post_tag' AND c.count = 0

DELETE a,b,c
FROM wp_terms AS a
LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN wp_term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id
WHERE c.taxonomy = ‘post_tag’ AND c.count = 0

2.刪除所有文章修訂版本(Revisions)以及它們的Meta數據

1
2
3
4
5
DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision'

DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = ‘revision’

3. 更改WordPress地址和首頁地址

1
2
3
UPDATE wp_options
SET option_value = replace(option_value, 'http://www.舊網址.com', 'http://www.新網址.com')
WHERE option_name = 'home' OR option_name = 'siteurl'

UPDATE wp_options
SET option_value = replace(option_value, ‘http://www.舊網址.com’, ‘http://www.新網址.com’)
WHERE option_name = ‘home’ OR option_name = ‘siteurl’

4. 更改文章的GUID

1
2
UPDATE wp_posts
SET guid = REPLACE (guid, 'http://www.舊網址.com', 'http://www.新網址.com')

UPDATE wp_posts
SET guid = REPLACE (guid, ‘http://www.舊網址.com’, ‘http://www.新網址.com’)

5. 更改正文中的鏈接地址

1
2
UPDATE wp_posts
SET post_content = REPLACE (post_content, 'http://www.舊網址.com', 'http://www.新網址.com')

UPDATE wp_posts
SET post_content = REPLACE (post_content, ‘http://www.舊網址.com’, ‘http://www.新網址.com’)

6. 更新文章的Meta值

1
2
UPDATE wp_postmeta
SET meta_value = REPLACE (meta_value, 'http://www.舊網址.com', 'http://www.新網址.com')

UPDATE wp_postmeta
SET meta_value = REPLACE (meta_value, ‘http://www.舊網址.com’, ‘http://www.新網址.com’)

7. 重設Admin密碼

1
2
3
UPDATE wp_users
SET user_pass = MD5( 'new_password' )
WHERE user_login = 'admin'

UPDATE wp_users
SET user_pass = MD5( ‘new_password’ )
WHERE user_login = ‘admin’

8. 重設admin的用戶名

1
2
3
UPDATE wp_users
SET user_login = 'newname'
WHERE user_login = 'admin'

UPDATE wp_users
SET user_login = ‘newname’
WHERE user_login = ‘admin’

9. 將作者a的文章全部轉移到作者b

1
2
3
UPDATE wp_posts
SET post_author = 'b'
WHERE post_author = 'a'

UPDATE wp_posts
SET post_author = ‘b’
WHERE post_author = ‘a’

10. 刪除文章的meta標簽

1
2
DELETE FROM wp_postmeta
WHERE meta_key = 'your-meta-key'

DELETE FROM wp_postmeta
WHERE meta_key = ‘your-meta-key’

11. 導出所有評論中的郵件地址

1
2
SELECT DISTINCT comment_author_email
FROM wp_comments

SELECT DISTINCT comment_author_email
FROM wp_comments

12. 刪除所有的Pingback

1
2
DELETE FROM wp_comments
WHERE comment_type = 'pingback'

DELETE FROM wp_comments
WHERE comment_type = ‘pingback’

13. 刪除所有的垃圾評論

1
2
DELETE FROM wp_comments
WHERE comment_approved = 'spam'

DELETE FROM wp_comments
WHERE comment_approved = ‘spam’

14. 禁用所有激活的插件

1
2
3
UPDATE wp_options
SET option_value = ''
WHERE option_name = 'active_plugins'

UPDATE wp_options
SET option_value = ”
WHERE option_name = ‘active_plugins’

15. 羅列所有未使用的Meta標簽

1
2
3
4
SELECT *
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE  wp.ID IS NULL

SELECT *
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL

16. 關閉舊文章的留言

1
2
3
UPDATE wp_posts
SET comment_status = 'closed'
WHERE post_date < '2009-01-01' AND post_status = 'publish'

UPDATE wp_posts
SET comment_status = ‘closed’
WHERE post_date < ‘2009-01-01’ AND post_status = ‘publish’

17. 更新留言者的網址

1
2
UPDATE wp_comments
SET comment_author_url = REPLACE( comment_author_url, 'http://舊網址.com', 'http://新網址.com' )

UPDATE wp_comments
SET comment_author_url = REPLACE( comment_author_url, ‘http://舊網址.com’, ‘http://新網址.com’ )

18. 更新正文內所有的’target=”_blank”‘為’rel=”nofollow”‘

1
2
UPDATE wp_posts
SET post_content = REPLACE (post_content, 'target="_blank',  'rel="nofollow')

UPDATE wp_posts
SET post_content = REPLACE (post_content, ‘target="_blank’, ‘rel="nofollow’)

以上18條來自 http://paranimage.com/19-wordpress-sql-hacks/,以後將繼續補充。

19.刪除未使用的Meta標簽

1
2
3
4
DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL

DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL

20.刪除重復的自定義字段(Custom Fields)

詳見:https://www.wpdaxue.com/remove-duplicate-custom-fields.html

21.更改文章自定義字段的名稱

1
UPDATE `wp_postmeta` SET `meta_key` = '新名稱' WHERE `meta_key` = '舊名稱';

UPDATE `wp_postmeta` SET `meta_key` = ‘新名稱’ WHERE `meta_key` = ‘舊名稱’;

發佈留言