Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressデータベース内のテキストを簡単に検索・置換する方法

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

長年にわたり、WordPressで大量の検索と置換を行う方法を多くの読者から質問されてきました。

WPBeginnerでは、大量のコンテンツを持つ大規模なWordPressサイトを管理することの難しさを理解しています。古いドメインからの移行であれ、リブランディングであれ、単に誤字脱字の修正であれ、すべての投稿、ページ、コメントするコンテンツを手作業で検索し、置き換えることはほぼ不可能です。

WordPressプラグインやSQLクエリーを使えば、WordPressサイトのテキスト、URL、画像を簡単に検索・置換することができます。

この投稿では、WordPressデータベース内のテキストを検索して置換する方法を紹介します。

Find and replace text in WordPress database with just one click

WordPressデータベースの検索と置換はいつ必要?

WordPressサイトのデータベース内の項目を検索し、置き換える必要がある場合がいくつかあります。

例えば、特定のテキストやURLをいくつかの投稿に追加したとします。どの投稿にそのテキストを追加したかはわからないが、たくさんの投稿があることは知っている。

手動でサイトを検索し、個別投稿をひとつずつ編集することもできる。しかし、それには多くの時間がかかるし、変更したいテキストを見逃してしまう可能性も高い。

個別コマンドを使用して自動的に項目を検索し、置き換えることで、同じことができますが、はるかに迅速かつ効率的です。

しかし、この方法には一つ欠点がある。ミスをした場合、元に戻すことができないのだ。一度置き換えたテキストは消えてしまうのです。探しているテキストと置き換えたいテキストを慎重に入力する必要があります。

それでは、WordPressデータベース内のテキストを簡単に検索・置換する方法を見ていきましょう。

データベースの検索と置換を実行する前に

先に述べたように、データベースに加えた変更は元に戻せません。データを失わないよう、十分に注意する必要があります。

まず、WordPressデータベースのバックアップを作成する必要があります。DuplicatorのようなWordPressバックアッププラグインを使用することで可能です。

また、phpMyAdminを使用してデータベースのバックアップを作成することもできます。

WordPressデータベースのバックアップを作成した後、findコマンドとreplaceコマンドを実行します。

WordPressプラグインで検索と置換を実行する

もしあなたがコードに詳しくなく、カスタムSQLクエリーを書きたくないのであれば、Search & Replace Everythingという使いやすい検索置換WordPressプラグインがある。

まず、無料のSearch & Replace Everythingプラグインをインストールして有効化する必要があります。詳しくはWordPressプラグインのインストール方法をご覧ください。

注: Search & Replace Everythingには、より高度な機能を備えたプレミアム版もあります。

プラグインを有効化したら、WordPress管理ダッシュボードのツール ” WP Search & Replaceページにアクセスする必要があります。

ここから、変更したいテキストを「Search for」フィールドに入力します。次に「Replace with」フィールドに置き換えたいテキストを入力します。

Enter the text that you want to search for and replace

また、WordPressデータベース内の特定のテーブルを選択するか、すべてを選択するかの設定も可能です。

次に、「検索と置換のプレビュー」ボタンを押します。

Select tables in Search & Replace Everything plugin

プラグインが適用する前に、変更を確認できるようになりました。

そして、「すべて置換」ボタンをクリックするだけです。

Click Replace All in Search & Replace Everything

WordPressで検索と置換を行う方法については、こちらをご覧ください。

phpMyAdminでMySQLクエリーの検索と置換を実行する

phpMyAdminを使ってWordPressデータベースからテキストを検索して置換することもできます。

まず、WordPressホスティングアカウントのダッシュボードにログインする必要があります。このチュートリアルでは、Bluehostを使用します。

サイトが表示されたら、左の列から「ウェブサイト」オプションをクリックし、「詳細設定」タブに切り替えます。

その後、’PHPMyAdmin’セクションまでスクロールダウンし、その横にある’Manage’ボタンをクリックします。

Click the Manage button next to the PHPMyAdmin section

新しいウィンドウでphpMyAdminが起動します。

ここでWordPressデータベース名をクリックし、「SQL」をクリックする。

SQL query in phpMyAdmin

この形式でSQLクエリーを入力する必要があります:

update TABLE_NAME set FIELD_NAME =
replace(FIELD_NAME, 'Text to find', 'text to replace with');

例えば、WordPressの投稿コンテンツ内のテキストを検索したい場合、次のようにクエリーを記述します:

update wp_posts set post_content =
replace(post_content,'Text to find','text to replace with');

そして、「Go」ボタンをクリックして続ける。

phpMyAdmin は SQL クエリーを実行し、成功するとクエリーによって影響を受けた行の数を表示します。

WordPressサイトにアクセスして、変更を確認できます。

この投稿が、WordPressデータベースのテキストをワンクリックで検索・置換する方法を学ぶのにお役に立てば幸いです。また、phpMyAdminを使ったWordPressデータベース管理の初心者ガイドや、専門家が選ぶ最高のWordPressデータベースプラグインもご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

19件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

    • WPBeginner Support says

      It would depend on where that code was added from, you should normally be able to edit the page that it is on for modifying the code.

      管理者

  2. Shahala says

    Hey there! could you please tell me how can I do this find and replace in sql for a word only in specific language?
    I tried using language queries but din’t work out!
    I really appreciate any help towards this. Thank you

    • WPBeginner Support says

      Unless I hear otherwise, we don’t have a language specific search at the moment sadly.

      管理者

    • WPBeginner Support says

      For the post and page titles, you would search the wp_posts table but, for changing categories and their urls you would want to do that manually and create 301 redirects.

      管理者

  3. RCdiy says

    I want to replace all instances of “OpenTx” with “OpenTX” but skipping URLs. So basically only what the viewer sees… How do I do this? I’ve installed both Search Replace and Better Search Replace but they also replace in the URLs…

  4. Piter says

    I tried plugins “Search $ replace” and “Better Search Replace” but there was no effect. Links in my posts was there. But…

    Thanks a lot for code metod, it works fine. It takes only few seconds.

  5. Oscar Estepa says

    I’m not 100% sure but I’d say the SQL query can’t handle serialised data. If you search & replace your database using the suggested SQL query you’ll break the serialised data and get yourself into lots of troubles down the road like widgets not working, images dissappearing, broken javascript, etc.

    It’s well explained in this link http://davidcoveney.com/782/mysql-database-search-replace-with-serialized-php/

    So better look for a plugin, script or tool which handles serialised data, like the suggested plugin Better Search Replace or the interconnectit script.

    Personally I use search and replace tool integrated in WP-CLI, in my experience it’s the best and fastest tool out there, never had a problem so far, tested in dozens of websites. For less savvy people, I’d recommend a plugin since WP-CLI is a terminal command tool and might not suite everyone.

  6. Damien Carbery says

    I like internetconnect/it’s Database Search and Replace Script.

    Unlike the phpMyAdmin example in the article, it will search all fields. It will also do a dummy run.

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。