If we restrict us to the output of wp post list
, then here’s a way to search for %test%
within the post titles of published posts:
wp post list --ignore_sticky_posts=1 --post__in=$(wp db query 'SELECT ID FROM wp_posts WHERE post_title LIKE "%test%" AND post_status="publish" AND post_type="post"' --skip-column-names | paste -s -d ',' - )
Here’s the expanded view:
wp post list #<-- the first wp command
--ignore_sticky_posts=1 #<-- get rid of stickies
--post__in= #<-- only specific post IDs
$(wp db query #<-- the second wp command
'SELECT ID #<-- only get the post IDs
FROM wp_posts #<-- table name, adjust the prefix!
WHERE post_title LIKE "%test%" #<-- title search word
AND post_status="publish" #<-- published
AND post_type="post"' #<-- posts
--skip-column-names #<-- Skip the ID column name
| paste -s -d ',' - #<-- csv
)
Update:
For exact cases, one can replace post_title LIKE "%test%"
with post_title = "test"
.
We can use --skip-column-names
to remove the ID
column name from the inner query output, instead of using sed 's/^ID,//'
to remove it. It would be nice if we could use --format=csv
instead of using | paste -s -d ',' -
, but currently that doesn’t seem to be supported on the wp db query
command.
There’s also the wp db search
command but currently we can only restrict it to a given table(s), like wp db search test wp_posts
, but not a given table column. One could e.g. try to play with wp db search test wp_posts --one_line | grep wp_posts:post_title
, i.e. search the whole wp_posts
table and filter the output that contains wp_posts:post_title
, thanks to --one_line
. But note this is searching the whole table first!
There’s also the wp shell
that prompts the wp-cli shell, where one has access to the WordPress classes and functions, like WP_Query
and get_page_by_title()
.
It’s also possible to the run wp eval
command and the wp evail-file
command to run a given php code. Here’s an example: wp eval 'print_r( get_page_by_title( "test" ) );