0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WordPress内のテーブルをまたいで置換をする

Posted at

WordPressはmeta情報なんかをシリアライズして持ってるので、SQLで直接アップデートがしにくいのだけれど、この世は地獄なので、どうしても置換したいことが出てくる。でも、毎日発生する作業ではなくて、すぐ忘れるため、ここに wp search-replace を記す。

参考はこの辺

# 必ず --dry-run を試す
# --regex つけると正規表現が使える
# これはdockerコンテナ内なのだけれど、rootで実行する場合、 --allow-root つけないと怒ってくる
root@945cd8d078a8:/var/www/html# wp search-replace 'http:\/\/example\.com' 'http://target.example.com' --regex --dry-run --allow-root
+------------------+-----------------------+--------------+------+
| Table            | Column                | Replacements | Type |
+------------------+-----------------------+--------------+------+
| wp_commentmeta   | meta_key              | 0            | PHP  |
| wp_commentmeta   | meta_value            | 0            | PHP  |
| wp_comments      | comment_author        | 0            | PHP  |
| wp_comments      | comment_author_email  | 0            | PHP  |
| wp_comments      | comment_author_url    | 0            | PHP  |
| wp_comments      | comment_author_IP     | 0            | PHP  |
| wp_comments      | comment_content       | 0            | PHP  |
| wp_comments      | comment_approved      | 0            | PHP  |
| wp_comments      | comment_agent         | 0            | PHP  |
| wp_comments      | comment_type          | 0            | PHP  |
| wp_links         | link_url              | 0            | PHP  |
| wp_links         | link_name             | 0            | PHP  |
| wp_links         | link_image            | 0            | PHP  |
| wp_links         | link_target           | 0            | PHP  |
| wp_links         | link_description      | 0            | PHP  |
| wp_links         | link_visible          | 0            | PHP  |
| wp_links         | link_rel              | 0            | PHP  |
| wp_links         | link_notes            | 0            | PHP  |
| wp_links         | link_rss              | 0            | PHP  |
| wp_options       | option_name           | 0            | PHP  |
| wp_options       | option_value          | 2            | PHP  |
| wp_options       | autoload              | 0            | PHP  |
| wp_postmeta      | meta_key              | 0            | PHP  |
| wp_postmeta      | meta_value            | 0            | PHP  |
| wp_posts         | post_content          | 19           | PHP  |
| wp_posts         | post_title            | 0            | PHP  |
| wp_posts         | post_excerpt          | 0            | PHP  |
| wp_posts         | post_status           | 0            | PHP  |
| wp_posts         | comment_status        | 0            | PHP  |
| wp_posts         | ping_status           | 0            | PHP  |
| wp_posts         | post_password         | 0            | PHP  |
| wp_posts         | post_name             | 0            | PHP  |
| wp_posts         | to_ping               | 0            | PHP  |
| wp_posts         | pinged                | 0            | PHP  |
| wp_posts         | post_content_filtered | 0            | PHP  |
| wp_posts         | guid                  | 69           | PHP  |
| wp_posts         | post_type             | 0            | PHP  |
| wp_posts         | post_mime_type        | 0            | PHP  |
| wp_term_taxonomy | taxonomy              | 0            | PHP  |
| wp_term_taxonomy | description           | 0            | PHP  |
| wp_termmeta      | meta_key              | 0            | PHP  |
| wp_termmeta      | meta_value            | 0            | PHP  |
| wp_terms         | name                  | 0            | PHP  |
| wp_terms         | slug                  | 0            | PHP  |
| wp_usermeta      | meta_key              | 0            | PHP  |
| wp_usermeta      | meta_value            | 0            | PHP  |
| wp_users         | user_login            | 0            | PHP  |
| wp_users         | user_nicename         | 0            | PHP  |
| wp_users         | user_email            | 0            | PHP  |
| wp_users         | user_url              | 0            | PHP  |
| wp_users         | user_activation_key   | 0            | PHP  |
| wp_users         | display_name          | 0            | PHP  |
+------------------+-----------------------+--------------+------+
# 置換実行
root@945cd8d078a8:/var/www/html# wp search-replace 'http:\/\/example\.com' 'http://target.example.com' --regex --allow-root
+------------------+-----------------------+--------------+------+
| Table            | Column                | Replacements | Type |
+------------------+-----------------------+--------------+------+
| wp_commentmeta   | meta_key              | 0            | PHP  |
| wp_commentmeta   | meta_value            | 0            | PHP  |
| wp_comments      | comment_author        | 0            | PHP  |
| wp_comments      | comment_author_email  | 0            | PHP  |
| wp_comments      | comment_author_url    | 0            | PHP  |
| wp_comments      | comment_author_IP     | 0            | PHP  |
| wp_comments      | comment_content       | 0            | PHP  |
| wp_comments      | comment_approved      | 0            | PHP  |
| wp_comments      | comment_agent         | 0            | PHP  |
| wp_comments      | comment_type          | 0            | PHP  |
| wp_links         | link_url              | 0            | PHP  |
| wp_links         | link_name             | 0            | PHP  |
| wp_links         | link_image            | 0            | PHP  |
| wp_links         | link_target           | 0            | PHP  |
| wp_links         | link_description      | 0            | PHP  |
| wp_links         | link_visible          | 0            | PHP  |
| wp_links         | link_rel              | 0            | PHP  |
| wp_links         | link_notes            | 0            | PHP  |
| wp_links         | link_rss              | 0            | PHP  |
| wp_options       | option_name           | 0            | PHP  |
| wp_options       | option_value          | 2            | PHP  |
| wp_options       | autoload              | 0            | PHP  |
| wp_postmeta      | meta_key              | 0            | PHP  |
| wp_postmeta      | meta_value            | 0            | PHP  |
| wp_posts         | post_content          | 19           | PHP  |
| wp_posts         | post_title            | 0            | PHP  |
| wp_posts         | post_excerpt          | 0            | PHP  |
| wp_posts         | post_status           | 0            | PHP  |
| wp_posts         | comment_status        | 0            | PHP  |
| wp_posts         | ping_status           | 0            | PHP  |
| wp_posts         | post_password         | 0            | PHP  |
| wp_posts         | post_name             | 0            | PHP  |
| wp_posts         | to_ping               | 0            | PHP  |
| wp_posts         | pinged                | 0            | PHP  |
| wp_posts         | post_content_filtered | 0            | PHP  |
| wp_posts         | guid                  | 69           | PHP  |
| wp_posts         | post_type             | 0            | PHP  |
| wp_posts         | post_mime_type        | 0            | PHP  |
| wp_term_taxonomy | taxonomy              | 0            | PHP  |
| wp_term_taxonomy | description           | 0            | PHP  |
| wp_termmeta      | meta_key              | 0            | PHP  |
| wp_termmeta      | meta_value            | 0            | PHP  |
| wp_terms         | name                  | 0            | PHP  |
| wp_terms         | slug                  | 0            | PHP  |
| wp_usermeta      | meta_key              | 0            | PHP  |
| wp_usermeta      | meta_value            | 0            | PHP  |
| wp_users         | user_login            | 0            | PHP  |
| wp_users         | user_nicename         | 0            | PHP  |
| wp_users         | user_email            | 0            | PHP  |
| wp_users         | user_url              | 0            | PHP  |
| wp_users         | user_activation_key   | 0            | PHP  |
| wp_users         | display_name          | 0            | PHP  |
+------------------+-----------------------+--------------+------+
Success: Made 90 replacements.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?