wpgrep - Search through a WordPress database directly
# search by SQL pattern % wpgrep --host ... --user ... --like '%Amelia%' # search by Perl pattern % wpgrep --host ... --user ... --regex '\bAmelia(?!Foo)' # search by arbitrary code % wpgrep --host ... --user ... --code Some::Module::subroutine # or combine them % wpgrep --host ... --user ... --like '%Amelia%' --regex '\bAmelia(?!Foo)' --code Some::Module::subroutine
I wanted a tool for complex searches of WordPress posts in my own installations. This is it. I can search by an SQL pattern, a Perl regular expression, or a any code I care to run on the values.
If you specify --like, it limits the returned rows to those whose post_title or post_content match that argument.
--like
post_title
post_content
If you specify --regex, it filters the returned rows to those whose post_title or post_content satisfy the regular expression.
--regex
If you specify --code, it filters the returned rows to those for which the subroutine reference returns true. The coderef gets a hash reference of the current row. It's up to you to decide what to do with it.
--code
These filters are consecutive. You can specify any combination of them but they always happen in that order. The --regex only gets the rows that satisfied the --like, and the --code only gets the rows that satisfied --like and --regex.
-c, --code
The fully-qualified name (e.g. Some::Module::subroutine) of a subroutine to run on each record. The program loads that module for you.
Be careful! This allows someone to run any code they like (and that's the point)!
-d, --db, --database
The database name. This is the DB_NAME in your wp-config.php.
DB_NAME
-h, --host
The database host. This defaults to localhost.
localhost
-l, --like
An SQL pattern suitable for a LIKE argument. The regex applies to the post_title and post_content.
See http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html
-p, --password
The database password associated with the user and the source machine, if you need that.
--port
The MySQL port, if you aren't using the default.
-r, --regex
A Perl regex used to filter the results. The regex applies to the post_title and post_content.
-u, --user
The MySQL user. You might want to set up a special read-only user for this tool.
WordPress::API
This source is in Github:
http://github.com/briandfoy/wordpress-grep/
brian d foy, <bdfoy@gmail.com>
<bdfoy@gmail.com>
Copyright (c) 2013, brian d foy, All Rights Reserved.
You may redistribute this under the same terms as Perl itself.
To install WordPress::Grep, copy and paste the appropriate command in to your terminal.
cpanm
cpanm WordPress::Grep
CPAN shell
perl -MCPAN -e shell install WordPress::Grep
For more information on module installation, please visit the detailed CPAN module installation guide.