WordPress::Grep - Search Wordpress titles and content
use WordPress::Grep; my $wp_grep = WordPress::Grep->connect( # required user => $user, database => $db, # optional password => $pass, # has defaults host => 'localhost', port => '3306', ); my $posts = $wp_grep->search( sql_like => '....', regex => qr/ ... /, code => sub { ... }, include_columns => [ ], # not implemented exclude_columns => [ ], # not implemented ); foreach my $post_id ( keys %$post ) { printf "%4d %s\n", $posts->{$post_id}{ID}, $posts->{$post_id}{post_title}; }
[This is alpha software.]
This module allows you to search through the posts in a WordPress database by directly examining the wp_posts table. Forget about these limited APIs. Use the power of Perl directly on the content.
wp_posts
I've long wanted this tool to examine consistency in my posts. I want to check my use of CSS and HTML across all posts to check what I may need to change when I change how I do things. This sort of thing is hard to do with existing tools and the WordPress API (although there is a WordPress::API.
I want to go through all posts with all the power of Perl, so my grep:
post_title
post_content
DBI
fetchall_hashref
ID
Right now, there are some limitations based on my particular use:
I assume UTF-8 everywhere, including in the database.
Applying a regex or code filter always return (at least) the post_title and post_content.
The LIKE and regex filters only work on post_title and post_content. The code filter gets the entire row as a hash reference and can do what it likes.
I've set up a slave of the MySQL server that runs my WordPress installations. In that slave, I set up a read-only user for this tool.
Connect to the WordPress database. You must specify these parameters, which should be the same ones in your wp_config.php (although if you need this tool frequently, consider setting up a read-only user for this, or run it against a slave).
user database
If you need a password, you'll have to provide that:
password
These parameters have defaults
host defaults to localhost port defaults to 3306
Return the db connection. This is a vanilla DBI connection to MySQL. If you subclass this, you can do further setup by overriding _db_init.
_db_init
The possible arguments:
sql_like - a string regex - a regular expression reference (qr//) code - a subroutine reference
This method first builds a query to search through the wp_posts table.
If you specify sql_like, it limits the returned rows to those whose post_title or post_content match that argument.
sql_like
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 sql_like, and the code only gets the rows that satisfied sql_like and regex.
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.