WP CLI说明文档

命令概述

用于列出并筛选 WordPress 数据库中的选项(options)。支持按名称搜索、排除、按自动加载状态过滤、显示临时选项等。

适合在需要查看、统计或批量操作选项时使用。

参数列表

参数 说明
--search=<pattern> 使用通配符(*和?)匹配选项名称。
--exclude=<pattern> 排除匹配的选项名称,支持通配符。
--autoload=<value> 当值为 on 时仅匹配自动加载选项,off 时仅匹配非自动加载选项。
--transients 仅列出临时选项。使用 --no-transients 忽略所有临时选项。
--unserialize 在输出中反序列化选项值。
--field=<field> 打印单个字段的值。
--fields=<fields> 将输出限制为特定对象字段。
--format=<format> 值的序列化格式。total_bytes 显示匹配选项的总字节数。默认:table。选项:table, json, csv, count, yaml, total_bytes。
--orderby=<fields> 按哪个字段排序。默认:option_id。选项:option_id, option_name, option_value。
--order=<order> 升序或降序。默认:asc。选项:asc, desc。

常用示例

获取所有自动加载选项的总大小

$ wp option list --autoload=on --format=total_bytes
33198

查找最大的临时选项

$ wp option list --search="*_transient_*" --fields=option_name,size_bytes | sort -n -k 2 | tail
option_name size_bytes
_site_transient_timeout_theme_roots 10
_site_transient_theme_roots 76
_site_transient_update_themes   181
_site_transient_update_core 808
_site_transient_update_plugins  6645

列出以 “i2f_” 开头的所有选项

$ wp option list --search="i2f_*"

+-------------+--------------+

删除所有以 “theme_mods_” 开头的选项

$ wp option list --search="theme_mods_*" --field=option_name | xargs -I % wp option delete %
Success: Deleted 'theme_mods_twentysixteen' option.
Success: Deleted 'theme_mods_twentyfifteen' option.
Success: Deleted 'theme_mods_twentyfourteen' option.