コンテンツにスキップ

WP-CLI の find コマンドの使い方

メインイメージ

WordPress のコマンドラインツール WP-CLI の find コマンド( wp find )について説明します。

WP-CLI の find コマンドとは?

WP-CLI の find コマンド(以下 wp find )は、ファイルシステム上にインストールされた WordPress サイトを探すためのコマンドです。

WordPress インストールディレクトリがどこにあるのかわからなくなった場合や、サーバーで複数の WordPress サイトを管理している場合などに有用です。

$ wp find .
+---------------------------------------+---------+-------+-------+
| version_path                          | version | depth | alias |
+---------------------------------------+---------+-------+-------+
| /var/www/html/wp-includes/version.php | 6.6.2   | 2     |       |
+---------------------------------------+---------+-------+-------+

インストール

wp find は WP-CLI 本退位は同梱されておらずパッケージという位置付けで配布されているため、利用する前に WP-CLI 本体とは別にインストールが必要です。

wp find をインストールするには wp package install コマンドを実行します。

wp package install wp-cli/find-command

WP-CLI 自体がインストールされていない場合は先に WP-CLI をインストールしてください。

参考ページ: WP-CLI のインストール方法

コマンドを実行して無事にインストール処理が完了すると、「 Success: Package intalled. 」というメッセージが最後に表示されます。

Installing package wp-cli/find-command (dev-main || dev-master || dev-trunk)
Updating /var/www/.wp-cli/packages/composer.json to require the package...
Using Composer to install the package...
---
Loading composer repositories with package information
Updating dependencies
Generating rules
Resolving dependencies through SAT
Looking at all rules.

Dependency resolution completed in 0.000 seconds
Analyzed 78 packages to resolve dependencies
Analyzed 81 rules to resolve dependencies
Lock file operations: 1 install, 0 updates, 0 removals
Installs: wp-cli/find-command:dev-main 48a7635
- Locking wp-cli/find-command (dev-main 48a7635)
Writing lock file
Installing dependencies from lock file
Package operations: 1 install, 0 updates, 0 removals
Installs: wp-cli/find-command:dev-main 48a7635
---
Success: Package installed.

ちなみに、私が PHP 8.2.24 の環境で実行したところ、次のような deprecation メッセージがたくさん出力されましたが、インストール自体は問題なく完了しました。

deprecation メッセージ
PHP Deprecated:  Return type of Symfony\Component\Process\Process::getIterator($flags = 0) should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in phar:///opt/wp-cli/bin/wp/vendor/symfony/process/Process.php on line 567

Deprecated: Return type of Symfony\Component\Process\Process::getIterator($flags = 0) should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in phar:///opt/wp-cli/bin/wp/vendor/symfony/process/Process.php on line 567
 - Installing <info>wp-cli/find-command</info> (<comment>dev-main 48a7635</comment>)

使い方

引数に探索対象のパスを指定して実行します。

wp find .

指定されたパスをサブディレクトリを再帰的に探索されて、見つかった WordPress サイトをリストアップされます。

デフォルトでは次のような出力がされます。

+---------------------------------------+---------+-------+-------+
| version_path                          | version | depth | alias |
+---------------------------------------+---------+-------+-------+
| /var/www/html/wp-includes/version.php | 6.6.2   | 2     |       |
+---------------------------------------+---------+-------+-------+

Note

wp find コマンドは内部的にはサブディレクトリに wp-includes/version.php を含むディレクトリを WordPress のインストールディレクトリ(ルートディレクトリ)だと認識しています。

出力のカスタマイズ

オプション --fields--format を指定すれば出力をカスタマイズできます。

wp find --fields=wp_path,version,db_host .
+----------------+---------+---------+
| wp_path        | version | db_host |
+----------------+---------+---------+
| /var/www/html/ | 6.6.2   | db      |
+----------------+---------+---------+

--fields で利用可能な値

意味
version_path version.php へのパス
version WordPress バージョン
depth インストールが見つかったディレクトリの深さ
alias 登録されている場合の WP-CLI エイリアス
wp_path WP-CLI の --path オプションに渡して使えるパス
db_host データベースホスト
db_user データベースユーザー
db_name データベース名

--format で利用可能な値

意味
table テーブル(デフォルト)
json JSON
csv CSV
yaml Yaml
count サイト数

サンプル:

table:

wp find --format=table .
+---------------------------------------+---------+-------+-------+
| version_path                          | version | depth | alias |
+---------------------------------------+---------+-------+-------+
| /var/www/html/wp-includes/version.php | 6.6.2   | 0     |       |
+---------------------------------------+---------+-------+-------+

json:

wp find --format=json .
[{"version_path":"\/var\/www\/html\/wp-includes\/version.php","version":"6.6.2","depth":0,"alias":""}]

csv:

wp find --format=csv .
version_path,version,depth,alias
/var/www/html/wp-includes/version.php,6.6.2,0,

yaml:

wp find --format=yaml .
---
-
  version_path: /var/www/html/wp-includes/version.php
  version: 6.6.2
  depth: 0
  alias: ""

count:

wp find --format=count .
1

wp_path は WP-CLI のグローバルオプション --path に渡して使えるので、例えば次のように複数の WordPress サイトに対して連続で同一のコマンドを実行するような用途に使えます。

# 指定されたディレクトリ以下のサイトをメンテナンスモードにする
wp find --fields=wp_path . | tail -n +2 | \
xargs -I {} wp maintenance-mode activate --path="{}"

確認時のバージョン

  • WordPress 6.6.2
  • WP-CLI 2.11.0

WordPress.org / GitHub 参考ページ

関連ページ