問題描述

每當我建立一個新網站時,我首先在像”stage.domain-name.com” 這樣的子域上建立一個分段站點。

一切正常後,我匯出資料庫,以 notepad ++開啟它,併為”subdomain.domain-name.com” 做一個查詢/替換,並將其替換為”domain-name.com” … 最後我將其匯入活動站點的新資料庫。

我的問題是… 我需要執行什麼 SQL 查詢,如果我只是想使用 phpmyadmin 在整個資料庫上做這個簡單的 find /replace?

-CH

最佳解決方案

儲存 URL 的表格是 wp_options 。您應該對使用您網站的網址的列進行更新:

UPDATE TABLE wp_options SET option_value = "new domain" WHERE option_name = "siteurl"
UPDATE TABLE wp_options SET option_value = "new domain" WHERE option_name = "home"

我可能會缺少一些價值,但是當您再次查詢/替換過程時,您可以注意到應該更新的值和表,並將其新增到此指令碼中。

WordPress Codex 有一個很好的指南,如何更改網站的 URL,也許這對你來說更為方便:Changing the Site URL

次佳解決方案

最好做選項,帖子,帖子內容和帖子元:

UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://olddomain.com','http://newdomain.com');

UPDATE wp_posts SET post_content = replace(post_content, 'http://olddomain.com', 'http://newdomain.com');

UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://olddomain.com', 'http://newdomain.com');

另請參閱 Searching Data using phpMyAdmin and MySQL | Packt Publishing.Search RegEx 是一個很好的 WP 外掛,能夠透過所有的帖子和頁面搜尋和替換 Grep 。

更新 6/16/2015:使用下一句中連結的工具好多了,因為在資料庫轉儲中的上述簡單查詢/替換將會破壞序列化資料。請參閱 interconnectit.com WordPress Serialized PHP Search Replace Tool. 這樣,您不會打破序列化資料,並且不需要在釋出內容上執行 RegEx,因為互連指令碼會隨處改變 URL 。我始終使用該工具將站點遷移到不同的域,或者簡單地從 http 到 https 進行全域性更改,以強制 SSL 無需外掛,並更改內容中的所有 URL,以防止不安全的元素錯誤。

第三種解決方案

這是一個偉大的 drop-in 指令碼,我使用它,它與 WP 使用的儲存選項的序列化陣列非常出色。完成後請務必將其從遠端伺服器中刪除,因為它具有巨大的安全風險。

https://interconnectit.com/products/search-and-replace-for-wordpress-databases/

第四種方案

你不必這樣做,你可以使用相對路徑。

當你連結的東西,而不是 subdomain.soemthing.com/image.jpg – 使用/image.jpg 例如

像這樣,你不會首先面對這個問題。

否則可以使用一個 mysql update 語句

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 『find this string』, 『replace found string with this string』);

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。