WP CLI说明文档

命令概述

在数据库表的文本列中搜索指定字符串,并输出彩色化的匹配结果。

默认搜索所有注册到 $wpdb 的表,多站点下仅限当前站点表。

参数列表

参数 说明
<search> 要搜索的字符串,默认不区分大小写。
[<tables>...] 一个或多个要搜索的表。
[--network] 在多站点中搜索所有注册到 $wpdb 的表。
[--all-tables-with-prefix] 搜索所有匹配表前缀的表,即使未注册到 $wpdb。覆盖 –network。
[--all-tables] 搜索数据库中所有表,忽略前缀。覆盖 –network 和 –all-tables-with-prefix。
[--before_context=<num>] 匹配前显示的字符数。默认:40。
[--after_context=<num>] 匹配后显示的字符数。默认:40。
[--regex] 将搜索作为正则表达式运行(无分隔符),区分大小写。
[--regex-flags=<regex-flags>] 为正则搜索传递 PCRE 修饰符(如 ‘i’ 表示不区分大小写)。
[--regex-delimiter=<regex-delimiter>] 正则表达式的分隔符,默认是 chr(1)。
[--table_column_once] 在每个表列的所有匹配行前只输出一次 ‘table:column’。
[--one_line] 将 ‘table:column’ 输出与行 ID 和匹配放在同一行。覆盖 –table_column_once。
[--matches_only] 仅输出匹配字符串(包括上下文),不输出表列和行 ID。
[--stats] 输出匹配数、耗时、搜索的表/列/行数等统计信息。
[--table_column_color=<color_code>] ‘table:column’ 输出的颜色代码。默认 ‘%G’(亮绿色)。
[--id_color=<color_code>] 行 ID 输出的颜色代码。默认 ‘%Y’(亮黄色)。
[--match_color=<color_code>] 匹配内容的颜色代码。默认 ‘%3%k’(芥末色背景黑色文字)。
[--fields=<fields>] 获取特定字段子集。
[--format=<format>] 输出格式,可选:table、csv、json、yaml、ids、count。

常用示例

搜索字符串

$ wp db search wordpress-develop

多站点子站搜索

$ wp db search example.com --url=example.com/foo

正则搜索并显示统计

$ wp db search 'https?://' --regex --stats

SQL 查询搜索

$ wp db query 'SELECT * FROM wp_options WHERE option_name like "%foo%"' --skip-column-names

以表格格式输出

$ wp db search https://localhost:8889 --format=table --fields=table,column,match

仅获取 ID

$ wp db search https://localhost:8889 wp_options --format=ids